httpClient post方法 解析json数据(向服务器传递,接受服务器传递)
生活随笔
收集整理的這篇文章主要介紹了
httpClient post方法 解析json数据(向服务器传递,接受服务器传递)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicHeader;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import org.apache.http.protocol.HTTP;import org.json.JSONException;import org.json.JSONObject;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.util.Log;import android.widget.TextView;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.util.ArrayList;import java.util.List;public class json extends Activity {public Context context;private TextView textView1;public static String URL = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";private DefaultHttpClient httpClient;StringBuilder result = new StringBuilder();private static final int TIMEOUT = 60;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);HttpParams paramsw = createHttpParams();httpClient = new DefaultHttpClient(paramsw);HttpPost post = new HttpPost("http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl");List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("name", "this is post"));try {//向服務(wù)器寫jsonJSONObject json = new JSONObject();Object email = null;json.put("email", email);Object pwd = null;json.put("password", pwd);StringEntity se = new StringEntity( "JSON: " + json.toString());se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));post.setEntity(se);post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));HttpResponse httpResponse = httpClient.execute(post);int httpCode = httpResponse.getStatusLine().getStatusCode();if (httpCode == HttpURLConnection.HTTP_OK&&httpResponse!=null) {Header[] headers = httpResponse.getAllHeaders();HttpEntity entity = httpResponse.getEntity();Header header = httpResponse.getFirstHeader("content-type");//讀取服務(wù)器返回的json數(shù)據(jù)(接受json服務(wù)器數(shù)據(jù))InputStream inputStream = entity.getContent();InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader reader = new BufferedReader(inputStreamReader);// 讀字符串用的。String s;while (((s = reader.readLine()) != null)) {result.append(s);}reader.close();// 關(guān)閉輸入流//在這里把result這個字符串個給JSONObject。解讀里面的內(nèi)容。JSONObject jsonObject = new JSONObject(result.toString());String re_username = jsonObject.getString("username");String re_password = jsonObject.getString("password");int re_user_id = jsonObject.getInt("user_id");setTitle("用戶id_"+re_user_id);Log.v("url response", "true="+re_username);Log.v("url response", "true="+re_password);} else {textView1.setText("Error Response" + httpResponse.getStatusLine().toString());}} catch (UnsupportedEncodingException e) {} catch (ClientProtocolException e) {} catch (IOException e) {} catch (JSONException e) {e.printStackTrace();} finally {if (httpClient != null) {httpClient.getConnectionManager().shutdown();// 最后關(guān)掉鏈接。httpClient = null;}}}public static final HttpParams createHttpParams() {final HttpParams params = new BasicHttpParams();HttpConnectionParams.setStaleCheckingEnabled(params, false);HttpConnectionParams.setConnectionTimeout(params, TIMEOUT * 1000);HttpConnectionParams.setSoTimeout(params, TIMEOUT * 1000);HttpConnectionParams.setSocketBufferSize(params, 8192 * 5);return params;}}
轉(zhuǎn)載于:https://my.oschina.net/yygh/blog/296409
總結(jié)
以上是生活随笔為你收集整理的httpClient post方法 解析json数据(向服务器传递,接受服务器传递)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取不到app.config里面的数据库
- 下一篇: angular学习笔记(二十五)-$ht