Back to project page Abstack-books-management.
The source code is released under:
MIT License
If you think the Android project Abstack-books-management listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.abstack.book.plugin.camera.util; /* w w w . ja v a 2 s .c o m*/ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.util.Log; public class HttpConnection { private static DefaultHttpClient client = null; static { client = new DefaultHttpClient(); // ClientConnectionManager mgr = client.getConnectionManager(); // HttpParams params = client.getParams(); // // client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, // mgr.getSchemeRegistry()), params); } /** * GET ???? * * @param url * @return */ public static String getRequest(String url) { String jsonStr = null; HttpGet httpRequest = new HttpGet(url); try { HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); HttpResponse httpResponse = client.execute(httpRequest); int status = httpResponse.getStatusLine().getStatusCode(); if (status == 200) { jsonStr = EntityUtils.toString(httpResponse.getEntity(), "gb2312"); } else { jsonStr = EntityUtils.toString(httpResponse.getEntity(), "gb2312"); } } catch (Exception e) { Log.e("exception", e.getMessage()); } return jsonStr; } /** * POST ???? * * @param url * @param map * @return result ??????????? */ public static String postRequest(String url, HashMap<String, String> map) { String result = null; Object obj = null; HttpPost httpRequest = new HttpPost(url); List<NameValuePair> listParams = new ArrayList<NameValuePair>(); Iterator<String> it = map.keySet().iterator(); try { HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); while (it.hasNext()) { obj = it.next(); if (obj != null && !obj.equals("")) { listParams.add(new BasicNameValuePair(obj.toString(), map .get(obj))); } } httpRequest.setEntity(new UrlEncodedFormEntity(listParams, HTTP.UTF_8)); HttpResponse httpResponse = client.execute(httpRequest); int code = httpResponse.getStatusLine().getStatusCode(); if (code == 200) { result = EntityUtils.toString(httpResponse.getEntity(), "gb2312"); } else { result = EntityUtils.toString(httpResponse.getEntity(), "gb2312"); } } catch (Exception e) { Log.e("exception", e.getMessage()); } return result; } /** * delete ?????? * * @param url * @return */ public static String deleteRequest(String url) { String result = null; HttpDelete httpDelete = new HttpDelete(url); try { HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); HttpResponse response = client.execute(httpDelete); if (response.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(response.getEntity(), "gb2312"); } else { result = EntityUtils.toString(response.getEntity(), "gb2312"); } } catch (Exception e) { Log.e("exception", e.getMessage()); } return result; } /** * put?????? * * @param url * ??map * @return */ public static String putRequest(String url, HashMap<String, String> map) { String result = null; Object obj = null; HttpPut httpPut = new HttpPut(url); List<NameValuePair> listParams = new ArrayList<NameValuePair>(); Iterator<String> it = map.keySet().iterator(); try { HttpParams params = client.getParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); while (it.hasNext()) { obj = it.next(); if (obj != null && !obj.equals("")) { listParams.add(new BasicNameValuePair(obj.toString(), map .get(obj))); } } httpPut.setEntity(new UrlEncodedFormEntity(listParams, HTTP.UTF_8)); HttpResponse response = client.execute(httpPut); if (response.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(response.getEntity(), "gb2312"); } else { result = EntityUtils.toString(response.getEntity(), "gb2312"); } } catch (Exception e) { Log.e("exception", e.getMessage()); } return result; } }