package
tuo.json;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.net.URI;
import
java.net.URISyntaxException;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
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.protocol.HTTP;
import
org.json.JSONArray;
import
org.json.JSONException;
import
org.json.JSONObject;
import
com.guc.android.nurse.activity.R.string;
public
class
JsonTruck {
public
String getJsonString(String wcfUrl)
throws
Exception {
HttpClient httpClient =
new
DefaultHttpClient();
HttpGet request =
new
HttpGet();
request.setURI(
new
URI(wcfUrl));
HttpResponse response = httpClient.execute(request);
return
parseHttpResponse(response);
}
public
String parseHttpResponse(HttpResponse response)
throws
Exception {
String jsonString=
""
;
int
status = response.getStatusLine().getStatusCode();
if
(status ==
200
) {
BufferedReader bReader =
new
BufferedReader(
new
InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb =
new
StringBuffer(
""
);
String line =
""
;
String NL = System.getProperty(
"line.separator"
);
while
((line = bReader.readLine()) !=
null
) {
sb.append(line + NL);
}
jsonString = sb.toString();
bReader.close();
}
return
jsonString;
}
public
JSONObject getJson(String wcfUrl,String jsonObjName)
throws
Exception {
String jsonString=getJsonString(wcfUrl);
JSONObject jsonObject =
new
JSONObject(jsonString);
return
jsonObject.getJSONObject(jsonObjName);
}
public
JSONArray getJsons(String wcfUrl,String jsonObjName)
throws
Exception{
JSONObject jsonObject=
new
JSONObject(getJsonString(wcfUrl));
return
jsonObject.getJSONArray(jsonObjName);
}
public
String doPost(String wcfUrl,JSONObject jsonObject)
throws
Exception {
HttpClient httpClient =
new
DefaultHttpClient();
HttpResponse response;
HttpPost post=
new
HttpPost();
HttpEntity httpEntity;
StringEntity stringEntity=
new
StringEntity(jsonObject.toString());
stringEntity.setContentEncoding(
new
BasicHeader(HTTP.CONTENT_TYPE,
"application/json"
));
httpEntity=stringEntity;
post.setEntity(httpEntity);
post.setURI(
new
URI(wcfUrl));
post.setHeader(
"Content-type"
,
"application/json"
);
response=httpClient.execute(post);
return
parseHttpResponse(response);
}
}
相关推荐
利用c# httpclient ,post json数据到web api
httpclient和RestfuL服务风格上传下载文件,可改为JAVA分布式系统进行交互,具体用法和改法详见代码里的注释
通过httpclient获取到JSON数据,展示到ListView通过httpclient获取到JSON数据,展示到ListView
Android客户端向服务器端请求数据的两种主要方式:HTTPURLConnection、HTTPClient;内含客户端与服务器端Demo(带有详细注释),原生方式解析json数据,希望大家多多批评指教!!
使用HttpURLConnection或HttpClient方式传json值到spring中
Android例子源码httpclient获取到JSON数据并展示到ListView
使用HttpClient4.3版本,使用Springmvc的restful方式访问后台的服务
该Demo主要介绍使用HttpClient获取网络数据,然后经过JSON后显示出来。另外本文章用到了常用的异步加载技术,AsyncTask类。
通过httpclient获取到JSON数据展示到ListView.zip
Android高级应用源码-通过httpclient获取到JSON数据,展示到ListView.zip
JAVAHttpClientThe server failed to respond with a validCaused by: org.apache.htt
HttpClient发送json、普通参数类型的Post请求(csdn)————程序
用VC实现http协议,构造httpserver和httpclient两端应用实例。
NULL 博文链接:https://cxl2012.iteye.com/blog/2210521
httpclient模拟post请求json封装表单数据的实现方法 httpclient模拟post请求json封装表单数据的实现方法
使用Arduino程式擷取Json檔案內的屬性資料,以至於後續物聯網開發。
使用c#实现的HttpClient拼接multipart/form-data形式参数post提交数据,包含图片内容,有需要的可以下载,希望能帮到有需要的人,
NULL 博文链接:https://javasam.iteye.com/blog/2117845
httpClient获取json格式的数据,并将数据赋值与自动化测试平台