怎么获取服务器端返回的数据,服务器端是一个接口
1个回答
展开全部
HttpClient httpClient = new DefaultHttpClient();
HttpPost post=new HttpPost(url);
HttpResponse response = null;
JSONArray array=null;
try {
//getHttpConnectionManager().getParams().setConnectionTimeout(11);
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
response = httpClient.execute(post);//发送请求
int statusCode=response.getStatusLine().getStatusCode();
System.out.println("连接状态:"+statusCode);
if (statusCode>= 200&&statusCode<400) {//判断请求是否成功
HttpEntity entity = response.getEntity();
String out = EntityUtils.toString(entity, "GBK");
//out=Base64.encodeToString(out.getBytes("GBK") ,Base64.DEFAULT);
System.out.println("返回结果:"+out);
array = new JSONArray(out);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("array"+array);
HttpPost post=new HttpPost(url);
HttpResponse response = null;
JSONArray array=null;
try {
//getHttpConnectionManager().getParams().setConnectionTimeout(11);
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
response = httpClient.execute(post);//发送请求
int statusCode=response.getStatusLine().getStatusCode();
System.out.println("连接状态:"+statusCode);
if (statusCode>= 200&&statusCode<400) {//判断请求是否成功
HttpEntity entity = response.getEntity();
String out = EntityUtils.toString(entity, "GBK");
//out=Base64.encodeToString(out.getBytes("GBK") ,Base64.DEFAULT);
System.out.println("返回结果:"+out);
array = new JSONArray(out);
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("array"+array);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |