在 Android Studio 中使用 GET 方法和 Volley 解析 JSON
Parsing JSON using GET method with Volley in Android Studio
我知道了 json
array
:
{
"conferences": [
{
"id": 1,
"name": "Conferencia Magistral",
"description": "Conferencia bien chingona lalala",
"speaker": "Jorge",
"biography": "bioo",
"place": {
"name": "Auditorio",
"description": "Presentacion de peliculas y documentales"
},
"date": "31/10/2015",
"time": "16:00"
}
]
}
这是我在 Android Studio:
中的代码
queue = Volley.newRequestQueue(this);
JsonArrayRequest JsonRequest = new JsonArrayRequest(Request.Method.GET, url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 1, count = response.length(); i < count; i++) {
EventInfo eventInfo = new EventInfo();
eventInfo.name = response.getJSONObject(i).getString("name");
eventInfo.date = response.getJSONObject(i).getString("date");
eventInfo.hour_event = response.getJSONObject(i).getString("time");
eventInfo.link="http://www.tallertoa.com/v1/files/gimgs/7_botanico-lamina-21.jpg";
eventInfo.description = response.getJSONObject(i).getString("description");
eventInfos.add(eventInfo);
}
recList.setAdapter(adapter);
Log.e("JSON", response.toString());
} catch (JSONException e) {
showErrorDialog("error Json parser", "error al parsear objeto Json Evento");
Log.e("json error",e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(JsonRequest);
}
所以,我无法解析这个 Json 数组,我已经这样做了,但是使用 POST 方法,所以我想知道你们中是否有人可以告诉我如何使用 volley
{
"conferences": [
{
"id": 1,
"name": "Conferencia Magistral",
"description": "Conferencia bien chingona lalala",
"speaker": "Jorge",
"biography": "bioo",
"place": {
"name": "Auditorio",
"description": "Presentacion de peliculas y documentales"
},
"date": "31/10/2015",
"time": "16:00"
}
]
}
首先,这是一个 JSONObject,而不是 JSONArray。您可以前往 here and here 了解更多信息
JSONObject: ...A string beginning with { (left brace) and ending with } (right brace).
JSONArray: ...A string that begins with [ (left bracket) and ends with ] (right bracket).
因此,您可以使用 JsonObjectRequest
而不是 JsonArrayRequest
希望对您有所帮助!
我知道了 json
array
:
{
"conferences": [
{
"id": 1,
"name": "Conferencia Magistral",
"description": "Conferencia bien chingona lalala",
"speaker": "Jorge",
"biography": "bioo",
"place": {
"name": "Auditorio",
"description": "Presentacion de peliculas y documentales"
},
"date": "31/10/2015",
"time": "16:00"
}
]
}
这是我在 Android Studio:
中的代码 queue = Volley.newRequestQueue(this);
JsonArrayRequest JsonRequest = new JsonArrayRequest(Request.Method.GET, url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 1, count = response.length(); i < count; i++) {
EventInfo eventInfo = new EventInfo();
eventInfo.name = response.getJSONObject(i).getString("name");
eventInfo.date = response.getJSONObject(i).getString("date");
eventInfo.hour_event = response.getJSONObject(i).getString("time");
eventInfo.link="http://www.tallertoa.com/v1/files/gimgs/7_botanico-lamina-21.jpg";
eventInfo.description = response.getJSONObject(i).getString("description");
eventInfos.add(eventInfo);
}
recList.setAdapter(adapter);
Log.e("JSON", response.toString());
} catch (JSONException e) {
showErrorDialog("error Json parser", "error al parsear objeto Json Evento");
Log.e("json error",e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(JsonRequest);
}
所以,我无法解析这个 Json 数组,我已经这样做了,但是使用 POST 方法,所以我想知道你们中是否有人可以告诉我如何使用 volley
{
"conferences": [
{
"id": 1,
"name": "Conferencia Magistral",
"description": "Conferencia bien chingona lalala",
"speaker": "Jorge",
"biography": "bioo",
"place": {
"name": "Auditorio",
"description": "Presentacion de peliculas y documentales"
},
"date": "31/10/2015",
"time": "16:00"
}
]
}
首先,这是一个 JSONObject,而不是 JSONArray。您可以前往 here and here 了解更多信息
JSONObject: ...A string beginning with { (left brace) and ending with } (right brace).
JSONArray: ...A string that begins with [ (left bracket) and ends with ] (right bracket).
因此,您可以使用 JsonObjectRequest
而不是 JsonArrayRequest
希望对您有所帮助!