コード例 #1
0
  public void postData(String url, String[] params) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);

    try {

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
      for (int i = 0; i < params.length; i++) {
        System.out.println(params[i]);
        String[] arr = StringHelpers.explode('=', params[i]);
        nameValuePairs.add(new BasicNameValuePair(arr[0], arr[1]));
      }

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

      HttpResponse response = httpclient.execute(httppost);
      InputStream is = response.getEntity().getContent();
      int length = (int) response.getEntity().getContentLength();
      listener.onReceivedResponse(is, length);

    } catch (ClientProtocolException e) {
      listener.onConnectionError(e);
    } catch (IOException e) {
      listener.onConnectionError(e);
    }
  }
コード例 #2
0
 public void cancel(Thread name) {
   System.out.println("canceling connection");
   name.interrupt();
   try {
     if (is != null) {
       is.close();
       is = null;
     }
     if (httpConn != null) {
       httpConn.disconnect();
       httpConn = null;
     }
     if (conn != null) {
       conn = null;
     }
     listener.onCancelledConnection();
   } catch (Exception e) {
     listener.onConnectionError(e);
   }
 }
コード例 #3
0
  public void setAndAccessURL(String urlString) {
    Log.i("tag", "akses mulai");
    //			while(isConnecting){
    try {
      if (Thread.interrupted()) listener.onCancelledConnection();

      int response = -1;

      URL url = new URL(urlString);
      conn = url.openConnection();

      if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection");

      HttpURLConnection httpConn = (HttpURLConnection) conn;
      httpConn.setAllowUserInteraction(false);
      httpConn.setInstanceFollowRedirects(true);
      httpConn.setRequestMethod("GET");
      httpConn.setConnectTimeout(60000);
      httpConn.connect();

      int length = httpConn.getContentLength();

      response = httpConn.getResponseCode();
      if (response == HttpURLConnection.HTTP_OK) {
        is = httpConn.getInputStream();

        if (listener != null) {
          Log.i("tag", "sukses");
          listener.onReceivedResponse(is, length);
        }
      } else {
        Log.e("tag", "not ok");
        listener.onConnectionResponseNotOk();
      }

      httpConn.disconnect();
      if (is != null) {
        is.close();
        is = null;
      }

    } catch (SocketTimeoutException ex) {
      if (listener != null) {
        Log.e("error", "connection timeout");
        listener.onConnectionTimeout();
      }
    } catch (InterruptedIOException ex) {
      if (listener != null) {
        listener.onCancelledConnection();
      }
    } catch (Exception ex) {
      if (listener != null) {
        Log.e("error", "error connecting to the internet");
        listener.onConnectionError(ex);
      }
    }
    //			}
  }