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 {
//向服务器写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;
}
}
分享到:
相关推荐
利用c# httpclient ,post json数据到web api
利用HttpClient获取数据并用Gson解析Json数据,内容比较简单,适合新手
httpclient模拟post请求json封装表单数据的实现方法 httpclient模拟post请求json封装表单数据的实现方法
NULL 博文链接:https://cxl2012.iteye.com/blog/2210521
android httpclient 访问服务器 获取json数据,里面有需要载入的外部jar包,添加源代码后再加入外部包就可。
Android客户端向服务器端请求数据的两种主要方式:HTTPURLConnection、HTTPClient;内含客户端与服务器端Demo(带有详细注释),原生方式解析json数据,希望大家多多批评指教!!
该Demo主要介绍使用HttpClient获取网络数据,然后经过JSON后显示出来。另外本文章用到了常用的异步加载技术,AsyncTask类。
httpclient post方式发送json格式数据,需要的相关包。
通过httpclient获取到JSON数据,展示到ListView通过httpclient获取到JSON数据,展示到ListView
现在C++也不行了吗,网上找不到相关代码。为了搞清楚C++中如何使用httpClient和Newtonsoft,研究了一下午。我从未用C++编程,一直用C#,现学现卖。
NULL 博文链接:https://zhcheng.iteye.com/blog/1292350
主要为大家详细介绍了C#使用Http Post方式传递Json数据字符串调用Web Service,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android例子源码httpclient获取到JSON数据并展示到ListView
小小测试项目包含了4个知识点,其中包含了http协议向服务器传输数据的post和get方法,包含了httpclient传输方法,httpclient上传文件到服务器的方法,
httpClient获取json格式的数据,并将数据赋值与自动化测试平台
采用httpclient发送xml报文,httpServer接收报文。两个程序一看就明白可以直接调用执行不在多说。
Android请求服务器和解析数据,包含8个实例代码:android_json,android访问网络返回的XML数据并且进行解析,httpclient获取到JSON数据并展示到ListView,Json数据解析服务器端加客户端代码等等。
通过httpclient获取到JSON数据展示到ListView.zip