Пример #1
0
  @Override
  public void run() {
    httpclient = new DefaultHttpClient();

    httppost = new HttpPost("http://arzoxadi.tk/niduc/bazadanych.php");
    // wymagane w AndroindManifest.xml <uses-permission
    // android:name="android.permission.INTERNET"></uses-permission>

    nameValuePairs = new ArrayList<NameValuePair>(2);

    // String string="";
    for (int i = 0; i < data.length; i++) {
      // string+=keys[i];
      // string+="=";
      // string+=data[i];
      // string+=";";
      nameValuePairs.add(new BasicNameValuePair(keys[i], data[i]));
    }
    // Log.i("http", string);

    try {
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      // Log.i("http", httppost.)
      HttpResponse execute = httpclient.execute(httppost);
      Log.i("http", execute.getEntity().getContent().toString());
      if (execute.getStatusLine().getStatusCode() == 200) {
        Log.i("http", "wyslano pomyslnie ".concat(httppost.toString()));
      } else {
        Log.i("http", "blad przy wysylaniu");
      }
    } catch (Exception e) {
      Log.i("http", e.getMessage());
    }
  }
Пример #2
0
 public static String makeOutboundRestWSCall(
     final PwmApplication pwmApplication,
     final Locale locale,
     final String url,
     final String jsonRequestBody)
     throws PwmOperationalException, PwmUnrecoverableException {
   final HttpPost httpPost = new HttpPost(url);
   httpPost.setHeader("Accept", PwmConstants.AcceptValue.json.getHeaderValue());
   if (locale != null) {
     httpPost.setHeader("Accept-Locale", locale.toString());
   }
   httpPost.setHeader("Content-Type", PwmConstants.ContentTypeValue.json.getHeaderValue());
   final HttpResponse httpResponse;
   try {
     final StringEntity stringEntity = new StringEntity(jsonRequestBody);
     stringEntity.setContentType(PwmConstants.AcceptValue.json.getHeaderValue());
     httpPost.setEntity(stringEntity);
     LOGGER.debug(
         "beginning external rest call to: " + httpPost.toString() + ", body: " + jsonRequestBody);
     httpResponse = PwmHttpClient.getHttpClient(pwmApplication.getConfig()).execute(httpPost);
     final String responseBody = EntityUtils.toString(httpResponse.getEntity());
     LOGGER.trace(
         "external rest call returned: "
             + httpResponse.getStatusLine().toString()
             + ", body: "
             + responseBody);
     if (httpResponse.getStatusLine().getStatusCode() != 200) {
       final String errorMsg =
           "received non-200 response code ("
               + httpResponse.getStatusLine().getStatusCode()
               + ") when executing web-service";
       LOGGER.error(errorMsg);
       throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg));
     }
     return responseBody;
   } catch (IOException e) {
     final String errorMsg =
         "http response error while executing external rest call, error: " + e.getMessage();
     LOGGER.error(errorMsg);
     throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg), e);
   }
 }