大佬教程收集整理的这篇文章主要介绍了如何从服务器下载JSON的示例?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这应该可以解决问题
String JsonResponse = httpHelper.connect(SERVER_URL);
JsONObject Json=new JsONObject(JsonResponsE);
private static String convertStreamToString(inputStream is) {
/*
* To convert the inputStream to String we use the BufferedReader.readline()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new inputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readline()) != null) {
sb.append(line + "\n");
}
} catch (IOException E) {
e.printstacktrace();
} finally {
try {
is.close();
} catch (IOException E) {
e.printstacktrace();
}
}
return sb.toString();
}
public static String connect(String url)
{
httpClIEnt httpclIEnt = new DefaulthttpClIEnt();
httpGet httpget = new httpGet(url);
httpResponse response;
try {
response = httpclIEnt.execute(httpget);
//Log.i(tag,response.getStatusline().toString());
httpentity entity = response.getEntity();
if (entity != null) {
inputStream instream = entity.getContent();
String result= convertStreamToString(instream);
instream.close();
return result;
}
} catch (ClIEntProtocolException E) {
} catch (IOException E) {
}
return null;
}
是否有一个很好的示例显示如何查询服务器并下载响应(JSON或XML)?
以上是大佬教程为你收集整理的如何从服务器下载JSON的示例?全部内容,希望文章能够帮你解决如何从服务器下载JSON的示例?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。