public static JSONObject post(String url,JSONObject json,Map<String, String> headers){ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); JSONObject response = null; post.setHeader("Content-Type", "application/json");if (headers != null) {Set<String> keys = headers.keySet();for (Iterator<String> i = keys.iterator(); i.hasNext();) {String key = (String) i.next();post.addHeader(key, headers.get(key));}} try { // json = new JSONObject();// json.put("Email", "testbuyer@buyercompany.com");;// json.put("Password", "123456"); StringEntity s = new StringEntity(json.toString(),"utf-8"); s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); //s.setContentType("application/json"); //s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // HttpResponse res = client.execute(post); HttpResponse httpResponse = client.execute(post);InputStream inStream = httpResponse.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); Log.i("MobilpriseActivity", strber.toString()); if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity entity = httpResponse.getEntity(); String charset = EntityUtils.getContentCharSet(entity); // response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset))); } } catch (Exception e) { throw new RuntimeException(e); } return response; }
Get method sends Header
/** * get * @param url * @param headerKey * @param headerVaue */private void getHttp(String url,Map<String, String> headers){httpGet = new HttpGet(url); if (headers != null) {Set<String> keys = headers.keySet();for (Iterator<String> i = keys.iterator(); i.hasNext();) {String key = (String) i.next();httpGet.addHeader(key, headers.get(key));}}HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); try {HttpResponse httpResponse = httpclient.execute(httpGet);InputStream inStream = httpResponse.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); Log.i("MobilpriseActivity", strber.toString());if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Log.i("MobilpriseActivity", "success");} } catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }
Httppost sends headers and Parameters
/** * * @param url the web will be connected * @param headers * @param parmas data will be sent */private void postHttp(String url,Map<String, String> headers,Map<String, String> parmas){httpPost = new HttpPost(url); if (headers != null) {Set<String> keys = headers.keySet();for (Iterator<String> i = keys.iterator(); i.hasNext();) {String key = (String) i.next();httpPost.addHeader(key, headers.get(key));}}ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();if (parmas != null) {Set<String> keys = parmas.keySet();for (Iterator<String> i = keys.iterator(); i.hasNext();) {String key = (String) i.next();pairs.add(new BasicNameValuePair(key, parmas.get(key)));}}HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpClient httpclient = new DefaultHttpClient(httpParameters); try {httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8")); // HttpResponse httpResponse = httpclient.execute(httpPost);InputStream inStream = httpResponse.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); Log.i("MobilpriseActivity", strber.toString());if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Log.i("MobilpriseActivity", "success");} } catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
Httppost sends headers, parameters, content (JSON format)
Note that the formats of the sent header and content are both Content-Type: Application/JSON.
The second method used for sending httppost is related to the previous one. comment out the post. setheader ("Content-Type", "application/JSON. setcontentencoding (New basicheader (HTTP. content_type, "application/JSON"); changed to S. setcontentencoding ("HTTP. utf_8 "); view the code
public static JSONObject post(String url,JSONObject json,Map<String, String> headers){ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); JSONObject response = null; // post.setHeader("Content-Type", "application/json");if (headers != null) {Set<String> keys = headers.keySet();for (Iterator<String> i = keys.iterator(); i.hasNext();) {String key = (String) i.next();post.addHeader(key, headers.get(key));}} try { json = new JSONObject(); json.put("Email", "testbuyer@buyercompany.com");; json.put("Password", "123456"); StringEntity s = new StringEntity(json.toString(),"utf-8"); // s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); s.setContentEncoding("HTTP.UTF_8"); //s.setContentType("application/json"); s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(s); // HttpResponse res = client.execute(post); HttpResponse httpResponse = client.execute(post);InputStream inStream = httpResponse.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); Log.i("MobilpriseActivity", strber.toString()); if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ HttpEntity entity = httpResponse.getEntity(); String charset = EntityUtils.getContentCharSet(entity); // response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(),charset))); } } catch (Exception e) { throw new RuntimeException(e); } return response; }