欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

httpClient post方法 解析json数据(向服务器传递,接受服务器传递)

发布时间:2025/4/16 编程问答 22 豆豆
生活随笔 收集整理的这篇文章主要介绍了 httpClient post方法 解析json数据(向服务器传递,接受服务器传递) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

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 {//向服务器写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");//读取服务器返回的json数据(接受json服务器数据)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();// 关闭输入流//在这里把result这个字符串个给JSONObject。解读里面的内容。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();// 最后关掉链接。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;}}



转载于:https://my.oschina.net/yygh/blog/296409

总结

以上是生活随笔为你收集整理的httpClient post方法 解析json数据(向服务器传递,接受服务器传递)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。