2015-11-10 34 views
0

我正在开发一个Android应用程序,我需要从远程服务器的MySQL数据库中获取结果。我写了一些PHP脚本来查询数据库并以JSON格式输出结果。Android - Volley对远程服务器的JSON请求只能正常工作

问题是请求仅在第一次获取预期结果。当我用不同的POST参数发送相同的请求时,它会再次返回第一个结果。我修改了PHP脚本以在JSON结果中附加POST参数,并尝试记录POST参数。这也是第一个参数。 如果我从设备上卸载应用程序并重新安装,它将再次仅在第一次返回正确的JSON。

注意:当我运行相同的代码与本地主机(WAMPP)上相同的PHP脚本和相同的数据库,一切工作完美。 我尝试使用谷歌浏览器扩展Postman来检查远程服务器的输出,它按预期工作。所以,我可以确认问题出在我的应用程序/代码上。

这里是我的CustomJSONObjectRequest类:

public class CustomJSONObjectRequest extends Request<JSONObject> { 

private Listener<JSONObject> listener; 
private Map<String, String> params; 

public CustomJSONObjectRequest(String url, Map<String, String> params, 
        Listener<JSONObject> reponseListener, ErrorListener errorListener) { 
    super(Method.GET, url, errorListener); 
    this.listener = reponseListener; 
    this.params = params; 
} 

public CustomJSONObjectRequest(int method, String url, Map<String, String> params, 
        Listener<JSONObject> reponseListener, ErrorListener errorListener) { 
    super(method, url, errorListener); 
    this.listener = reponseListener; 
    this.params = params; 
} 

protected Map<String, String> getParams() 
     throws com.android.volley.AuthFailureError { 
    return params; 
}; 

@Override 
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { 
    try { 
     String jsonString = new String(response.data, 
       HttpHeaderParser.parseCharset(response.headers)); 
     return Response.success(new JSONObject(jsonString), 
       HttpHeaderParser.parseCacheHeaders(response)); 
    } catch (UnsupportedEncodingException e) { 
     return Response.error(new ParseError(e)); 
    } catch (JSONException je) { 
     return Response.error(new ParseError(je)); 
    } 
} 

@Override 
protected void deliverResponse(JSONObject response) { 
    listener.onResponse(response); 
} 
} 

这是怎么了发送请求:

  String item_name = getIntent().getExtras().getString("item_name"); 
      String url = getString(R.string.remote_server)+"fetch-item-list.php"; 

      CustomJSONObjectRequest jsObjRequest = new CustomJSONObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 


        try { 
         Log.d("item name from intent extra", item_name); //intent extra 
         Log.d("response", response.toString()); //json response 
         Log.d("item name from response", response.getString("item_name")); //post parameter. This and the intent extra should be same 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show(); 
       } 
      }) { 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> params = new HashMap<>(); 

        params.put("item_name", item_name); 

        return params; 
       } 

       @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        Map<String, String> header = new HashMap<String, String>(); 
        header.put("Content-Type", "application/json; charset=utf-8"); 
        return header; 
       } 
      }; 



      MyVolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsObjRequest); 

这是logcat的:

D/item name from intent extra: apple 
D/Response: {"data":[{"item_discount":"0.00","item_price":"50.00","unique_id":"181................ 
D/item name from response: orange 
+0

我没有找到错误,但新的CustomJSONObjectRequest(Request.Method.POST,这里你使用POST和这里使用超级(Method.GET,url,errorListener); GET为什么是这样? – Amsheer

+0

检查你的服务器应用程序,橙'是默认的项目名称reponsed,如果它没有得到任何item_name – BNK

+0

@Amsheer我打电话给第二个构造函数,该方法作为参数,并调用超级传递它 – amzer

回答

0

我想我找到解决方案

看起来像JSON响应得到缓存,所以第一个请求的响应被缓存,并且下一个请求从未发送到服务器。缓存的JSON响应已针对其他任何请求返回。

我只需要禁用缓存。 在添加到请求队列之前,我添加了行jsObjRequest.setShouldCache(false);

jsObjRequest.setShouldCache(false); 
MyVolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsObjRequest); 

感谢此question

但我仍然不明白为什么它没有这个设置在localhost上工作。

+1

这是因为服务器响应支持输出缓存,你可以在这里阅读更多信息https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl= en#cache-control – BNK

+0

是啊!感谢您的链接。 – amzer

相关问题