我在android中连接到一个带排球的网址:
private void connectToUrl(String url) {
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(response.toString());
JSONArray contacts = jsonObj.getJSONArray("d");
for (int i = 0; i < contacts.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
Log.i("VALUEDSSDDD", object.getString("urlpic"));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Custom Log Error", "Error: " + error.getMessage());
Log.i("ERRRROOORR",error.getMessage()+"");
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_arry);
req.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
但是当我连接到url
时,我会收到错误:
org.json.JSONException: Value .....
这是我的json
:
{
"d": [{
"apiCurrentUserID": 0,
"apiCurrentUserName": null,
"urlpic": "http://Image.jpg",
"Title": "Jim",
"FileMusicUrl": "http://video.mp4",
"StringDuration": "05':07''",
"ViewCount": 57,
"DownloadCount": 56,
"uploadDate": "/Date(1473550560)/",
"MusicID": 192727,
"UserID": 0,
"Summary": "Some text",
"OrderView": null,
"ShowDate": "/Date(1473550500)/",
"FileSize": "19.15 MB",
"RatePlus": null,
"RateDash": null,
"ChannelID": 0,
"ChannelName": null,
"CurrentChannelID": 0,
"CurrentChannelName": null,
"isFollowed": false,
"profileImgUserSend": null,
"isDisliked": false,
"isLiked": false,
"isLatered": false,
"isFavorited": false,
"apiCategories": null,
"apiTags": [{
"tageID": "438232",
"titleTag": "book"
}, {
"tageID": "411557",
"titleTag": "sounds"
}, {
"tageID": "365984",
"titleTag": "map"
}]
}],
"RowwCount": 0
}
我从json
获得url
以上。
编辑:
我将我的代码编辑为:
private void connectToUrl(String url) {
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.DEPRECATED_GET_OR_POST, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(response.toString());
JSONObject contacts = jsonObj.getJSONObject("d");
try {
Log.i("VALUEDSSDDD", contacts.getString("urlpic"));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "Error " + error.getMessage());
}
}
);
requestQueue.add(obreq);
}
但是给我一个错误:
null
在最后一段代码中显示结果,但在此代码中没有任何结果。
答案 0 :(得分:0)
您粘贴的响应不是JSONArray它是JSONObject。您需要获取d的值,即JSON数组。
答案 1 :(得分:0)
我的问题已解决:
private void connectToUrl(String url) {
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(response.toString());
JSONArray contacts = jsonObj.getJSONArray("d");
for (int i = 0; i < contacts.length(); i++) {
try {
JSONObject object = contacts.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Custom Log Error", "Error: " + error.getMessage());
Log.i("ERRRROOORR",error.getMessage()+"");
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req, tag_json_arry);
req.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}