Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.androidhive.slidingmenu;
- import android.os.AsyncTask;
- import android.util.Log;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.params.ConnManagerParams;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.HttpConnectionParams;
- import org.apache.http.params.HttpParams;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.jar.Attributes;
- /**
- * Created by jamesbond on 10/10/2014.
- */
- public class CustomHttpClient extends AsyncTask<ArrayList<NameValuePair>, String, String> {
- /** The time it takes for our client to timeout */
- ArrayList<NameValuePair> arrayList;
- String url;
- public CustomHttpClient(ArrayList<NameValuePair> arrayList, String url){
- this.arrayList = arrayList;
- this.url = url;
- }
- @Override
- protected String doInBackground(ArrayList<NameValuePair>... params) {
- BufferedReader in = null;
- String result = null;
- try {
- int HTTP_TIMEOUT = 30 * 1000; // milliseconds
- /** Single instance of our HttpClient */
- HttpClient mHttpClient = null;
- if (mHttpClient == null) {
- mHttpClient = new DefaultHttpClient();
- final HttpParams paramss = mHttpClient.getParams();
- HttpConnectionParams.setConnectionTimeout(paramss, HTTP_TIMEOUT);
- HttpConnectionParams.setSoTimeout(paramss, HTTP_TIMEOUT);
- ConnManagerParams.setTimeout(paramss, HTTP_TIMEOUT);
- }
- HttpClient client = mHttpClient;
- HttpPost request = new HttpPost(url);
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(arrayList);
- request.setEntity(formEntity);
- HttpResponse response = client.execute(request);
- in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- result = sb.toString();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement