Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. package info.androidhive.slidingmenu;
  2.  
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5.  
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.entity.UrlEncodedFormEntity;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.conn.params.ConnManagerParams;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.params.HttpConnectionParams;
  14. import org.apache.http.params.HttpParams;
  15.  
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. import java.util.ArrayList;
  20. import java.util.jar.Attributes;
  21.  
  22. /**
  23. * Created by jamesbond on 10/10/2014.
  24. */
  25. public class CustomHttpClient extends AsyncTask<ArrayList<NameValuePair>, String, String> {
  26. /** The time it takes for our client to timeout */
  27. ArrayList<NameValuePair> arrayList;
  28. String url;
  29. public CustomHttpClient(ArrayList<NameValuePair> arrayList, String url){
  30. this.arrayList = arrayList;
  31. this.url = url;
  32. }
  33. @Override
  34. protected String doInBackground(ArrayList<NameValuePair>... params) {
  35. BufferedReader in = null;
  36. String result = null;
  37. try {
  38. int HTTP_TIMEOUT = 30 * 1000; // milliseconds
  39.  
  40. /** Single instance of our HttpClient */
  41. HttpClient mHttpClient = null;
  42. if (mHttpClient == null) {
  43. mHttpClient = new DefaultHttpClient();
  44. final HttpParams paramss = mHttpClient.getParams();
  45. HttpConnectionParams.setConnectionTimeout(paramss, HTTP_TIMEOUT);
  46. HttpConnectionParams.setSoTimeout(paramss, HTTP_TIMEOUT);
  47. ConnManagerParams.setTimeout(paramss, HTTP_TIMEOUT);
  48. }
  49. HttpClient client = mHttpClient;
  50. HttpPost request = new HttpPost(url);
  51. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(arrayList);
  52. request.setEntity(formEntity);
  53. HttpResponse response = client.execute(request);
  54. in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  55. StringBuffer sb = new StringBuffer("");
  56. String line = "";
  57. String NL = System.getProperty("line.separator");
  58. while ((line = in.readLine()) != null) {
  59. sb.append(line + NL);
  60. }
  61. in.close();
  62. result = sb.toString();
  63.  
  64. }
  65. catch (Exception e)
  66. {
  67. e.printStackTrace();
  68. }
  69. finally {
  70. if (in != null) {
  71. try {
  72. in.close();
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77. }
  78. return result;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement