如何获得JSON的任何URL响应为Android,比效应初探之后,我想它解析 [英] how to get response from any url in json for android and than after reponse i want to parse it
问题描述
我想知道如何JSON工作,以及我们如何能得到回应的形式网址,比后,我要分析此效应初探。所以请告诉我一些链接,将有益的给我这个conpect
i want to know about how json works and how we can get response form url and than after i want to parse this reponse. so please tell me some link which will helpfull to me for this conpect
推荐答案
要了解JSON阅读此链接。
to understand json read this link..
http://secretgeek.net/json_3mins.asp
您可以找到JSON教程在Android
you can find "json tutorials" on android
你要做的第一件事就是获得响应文本。
first thing you have to do is to get the response text.
private String getResponseText(String stringUrl) throws IOException
{
StringBuilder response = new StringBuilder();
URL url = new URL(stringUrl);
HttpURLConnection httpconn = (HttpURLConnection)url.openConnection();
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192);
String strLine = null;
while ((strLine = input.readLine()) != null)
{
response.append(strLine);
}
input.close();
}
return response.toString();
}
在这之后,你将采取的应对文本,并解析它作为一个JSON根对象,像这样
In which after that you will take the response text and parse it as a json root object like so
String responseText = GetResponseText(requestUrl);
JSONObject mainResponseObject = new JSONObject(responseText);
和然后根据你以下类解析的JSONObject的数据的结构:
的JSONObject JSONArray
and then according to the structure of the data you parse the JSONObject with the following classes: JSONObject JSONArray
和使用的类中定义的get方法获取值,检查他们的文件上。
and get the values using the get methods defined on the classes, check them on the documentation.
http://developer.android.com/reference/org/json/ JSONObject.html
有很多在网上例子并搜寻他们。
there are a lot of examples on the net do search for them..
这篇关于如何获得JSON的任何URL响应为Android,比效应初探之后,我想它解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!