httpclient get请求返回的数据乱码?跪求大神帮帮我..下面是代码,就是一个调用接口返回天气预报json数据 5
publicclassweatherTest{publicstaticvoidmain(String[]args)throwsException{Stringu=URLE...
public class weatherTest {
public static void main(String[] args) throws Exception {
String u = URLEncoder.encode("重庆","UTF-8");
String url = "http://wthrcdn.etouch.cn/weather_mini?city="+u;
String post = get(url);
System.out.println(post);
}
public static String get(String url){
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
String json = null;
try{
HttpResponse res = client.execute(get);
if(res.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
json = EntityUtils.toString(entity);
}
}catch(Exception e){
throw new RuntimeException();
}finally{
client.getConnectionManager().shutdown();
}
return json;
} 展开
public static void main(String[] args) throws Exception {
String u = URLEncoder.encode("重庆","UTF-8");
String url = "http://wthrcdn.etouch.cn/weather_mini?city="+u;
String post = get(url);
System.out.println(post);
}
public static String get(String url){
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
String json = null;
try{
HttpResponse res = client.execute(get);
if(res.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
HttpEntity entity = res.getEntity();
json = EntityUtils.toString(entity);
}
}catch(Exception e){
throw new RuntimeException();
}finally{
client.getConnectionManager().shutdown();
}
return json;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询