在Android中解析以下响应时遇到问题:
{"success":true,"msg":"Okay","result":{"content_type_id":"2","Content_Type_Name":"Media"}}
我需要检索 content_type_id ,因此我可以在set方法中使用它。
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// This is where I'm stuck.
}
我收到的一些错误are not able to convert to JSONArray
和no value for
。
我该如何解决这个问题?
答案 0 :(得分:1)
使用以下代码,
在 int numberOfThreads = 0;
int seconds = 0;
List<System.Threading.Thread> timer = new List<System.Threading.Thread>();//(timex(6))>();
//create many threads together
for (int i = 0; i < numberOfThreads; i++)
{
System.Threading.Thread tempx = new System.Threading.Thread(timex(seconds));
timer.Add(tempx);
tempex = null;
timer[i].Start();
}
//each time a timer is needed
System.Threading.Thread temp = new System.Threading.Thread(timex(seconds));
temp.Start();
private System.Threading.ThreadStart timex(int seconds)
{
for (int i = 0; i < seconds; i++)
{
//your code here
System.Threading.Thread.Sleep(1000);
}
MessageBox.Show("A service has finished");
return null;
}
方法中编写此代码。
onResponse
希望它会对你有所帮助。
答案 1 :(得分:0)
您的回复中没有JSONArray。转换为JSONObject。
JsonObject jsonObject=new JsonObject(response);
答案 2 :(得分:0)
使用JsonObjectRequest
替换为StringRequest
;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, jsonObj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
printlog("response", response + "");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
callback.serviceCallback(callbackId, null);
String message = null;
if (volleyError instanceof NetworkError) {
message = "Cannot connect to Internet...Please check your connection!";
Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show();
} else if (volleyError instanceof ServerError) {
message = "The server could not be found. Please try again after some time!!";
Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show();
} else if (volleyError instanceof AuthFailureError) {
message = "Cannot connect to Internet...Please check your connection!";
Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show();
}
else if (volleyError instanceof NoConnectionError) {
message = "Cannot connect to Internet...Please check your connection!";
Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show();
} else if (volleyError instanceof TimeoutError) {
message = "Connection TimeOut! Please check your internet connection.";
Toast.makeText(getInstance(),message,Toast.LENGTH_SHORT).show();
}
}
});
jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(5000 * 60, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
try {
getInstance().addToRequestQueue(jsonObjReq, url);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
答案 3 :(得分:0)
其他人正在走上正轨,但你需要更进一步。 Vishal Chhodwani也有一个很好的答案,它包括一些验证。
首先,按照他们的建议将响应转换为JSONObject。然后从响应中检索结果作为JSONObject。最后,从结果中获取content_type_id。
JSONObject jsonResponse = new JSONObject(response);
JSONObject jsonResult = jsonResponse.getJSONObject("result");
String contentTypeID = jsonResult.getString("content_type_id");
此外,如果您希望 content_type_id 为您的set方法的int
,则可以尝试以下方法:
int contentTypeID = jsonResult.getInt("content_type_id");
有关更多详细信息,请参阅JSONObject的Android文档链接:https://developer.android.com/reference/org/json/JSONObject.html