JSON在Android中 [英] JSON in Android
本文介绍了JSON在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要消耗一个JSON Web服务,
I want to consume a json web service ,
这是网址: http://10.0.2.2:8888/RESULT.JSON
{
"idpersonne":1,
"nom":"AAA",
"prenom":"AAA",
}
我怎么能检索对象的人采用了android?
how I can retrieve the object "person" using android ?
推荐答案
下面:
final URL url = new URL(path);
InputStream is = url.openStream();
final String jString = convertStreamToString(is);
final JSONObject jObject = new JSONObject(jString);
final String jString = convertStreamToString(is);
final JSONObject jObject = new JSONObject(jString);
long personId = jObject.getLong("idpersonne");
String nom = jObject.getString("nom");
String prenom = jObject.getString("prenom");
convertStreamToString方式:
convertStreamToString method:
private String convertStreamToString(final InputStream is)
{
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final StringBuilder stringBuilder = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\n");
}
} catch (final IOException e)
{
L.e(e.getMessage());
} finally
{
try
{
is.close();
} catch (final IOException e)
{
L.e(e.getMessage());
}
}
return stringBuilder.toString();
}
这篇关于JSON在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文