Java 带有Singleton类的嵌套截取请求返回null
我的代码中有一个错误。外部请求返回一个数据,但内部循环返回null 我在这里做的是:我请求一些数据,然后再次使用我从第一个请求中获得的id,我使用它发送另一个请求。虽然我收到了第一个响应,但在第二个嵌套请求中收到了ERRORNull消息 我确信url是正确的。我一直没能找到解决这个问题的办法Java 带有Singleton类的嵌套截取请求返回null,java,android,android-volley,Java,Android,Android Volley,我的代码中有一个错误。外部请求返回一个数据,但内部循环返回null 我在这里做的是:我请求一些数据,然后再次使用我从第一个请求中获得的id,我使用它发送另一个请求。虽然我收到了第一个响应,但在第二个嵌套请求中收到了ERRORNull消息 我确信url是正确的。我一直没能找到解决这个问题的办法 private ArrayList<Item> fetchApiData(){ String url="http://www.gadgetsinnepal.com.np/wp-json
private ArrayList<Item> fetchApiData(){
String url="http://www.gadgetsinnepal.com.np/wp-json/wp/v2/posts/";
JsonArrayRequest jsArrayRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
// Parsing json array response
// loop through each json object
for (int i = 0; i < response.length(); i++) {
JSONObject item = (JSONObject) response
.get(i);
String id = item.getString("id");
String date = item.getString("date");
JSONObject titleobj = item
.getJSONObject("title");
String title= titleobj.getString("rendered");
String featuredMedia= item.getString("featured_media");
Toast.makeText(getApplicationContext(), "ID :" + id +" Date: "+ date+ " Title "+ title + featuredMedia,
Toast.LENGTH_SHORT).show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
"http://www.gadgetsinnepal.com/wp-json/wp/v2/media/"+featuredMedia, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject nested_response) {
try {
// Parsing json object response
// response will be a json object
JSONObject guilld = nested_response.getJSONObject("guid");
String featured_img_url = guilld.getString("rendered");
String nestid=nested_response.getString("id");
Toast.makeText(getApplicationContext(),nested_response.toString()+"IMAGE" + nestid,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"ERROR"+error.getMessage(), Toast.LENGTH_LONG).show();
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjReq);
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
}
private ArrayList fetchApiData(){
字符串url=”http://www.gadgetsinnepal.com.np/wp-json/wp/v2/posts/";
JsonArrayRequest jsArrayRequest=新JsonArrayRequest
(Request.Method.GET,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
试一试{
//解析json数组响应
//循环遍历每个json对象
对于(int i=0;i
通过仔细检查错误日志给出的位置,该问题得以解决。
在方法中具有唯一的日志将使查找问题发生的位置更容易
在这种情况下,我们发现在以下方面发生了一些事情:
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"ERROR"+error.getMessage(), Toast.LENGTH_LONG).show();
}
进一步调查显示我们收到了一条回复代码503的信息。
发生这种情况的原因:
增加请求的超时时间似乎可以防止再次发生这种情况。您是否知道您正在使用此函数和此函数中的getApplicationContext调用Singleton的getInstance?他们不一样。对不起,我不知道。我应该写什么来代替“getApplicationContext”?你可以把它们都写进getApplicationContext。我写了。我仍然收到ERRORnull toast消息。好的,您描述的唯一ERRORnull消息似乎来自onErrorResponse(截击错误)。在将来,当你制作错误日志时,考虑添加一个唯一的字符串,以确定它的错误日志。从逻辑上考虑,截击错误对象为空。也许它没有被正确地创建。试着调试一下。