服务器接收网页数据传输实现,httpClient post方法 解析json数据(向服务器传递,接受服务器传递)...

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 params = new ArrayList();

params.add(new BasicNameValuePair("name", "this is post"));

try {

//向服务器写json

JSONObject 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;

}

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值