[英]Get Request JsonArray with volley
我遵循了这个问题,可以在不同的活动之间重复使用截击服务,但是他只是将JSONOBject用于GET和POST请求,因此我需要返回JSONArray,因为我的请求会返回1项以上的内容。
所以我在Volley Service上有这样的事情:
getDataVolley(final String requestType, String url){
Log.d("TRIED","TRIED0");
try {
Log.d("TRIED","TRIED");
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonArrayRequest jsonArray = new JsonArrayRequest(Request.Method.GET, url,null, new Response.Listener
() {
@Override
public void onResponse(JSONArray response) {
Log.d("TRIED","TRIED2");
if(mResultCallback != null)
mResultCallback.notifySuccess(requestType, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("TRIED",error.toString());
}
});
jsonArray.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonArray);
}catch(Exception e){
Log.d("TRIED","TRIED4");
}
}`
所以我在这样的主要活动上使用此服务:
初始化
initPlants();
Log.d("RESULTCALL",mResultCallback.toString());
mVolleyService = new VolleyService(mResultCallback,this);
mVolleyService.getDataVolley(GETREQUEST,URL);
打回来
void initPlants(){
mResultCallback = new IResult() {
@Override
public void notifySuccess(String requestType, JSONArray response) {
}
@Override
public void notifyError(String requestType,VolleyError error) {
Log.d("GJJJ","GJJJ1");
}
};
}
public void showToast(String message){
Toast toast = Toast.makeText(SimiliarPhotos.this,message, Toast.LENGTH_LONG);
toast.show();
}
问题是我在response(volleyService)的第二个参数中收到错误,说它需要一个JsonObject。
我的IResult需要JSONObject而不是JSONArray
像这样修改您的界面
public interface IResult {
public void notifySuccess(String requestType,JSONObject response);
public void notifySuccess(String requestType,JSONArray response);
public void notifyError(String requestType,VolleyError error);
}
// added a new method "notifySuccess" where params are requestType & JSONArray response
现在,从您称为抽射的地方开始您的活动。 从“ onCreate()”中删除“ initPlants();” 方法。 这样写你的活动名称
public class Your_Activity extends Activity implements IResult {
现在实现类似的覆盖方法
@Override
public void notifySuccess(String requestType, JSONObject response) {
}
@Override
public void notifySuccess(String requestType, JSONArray response) {
// Here You'll receive Your response as Array. Retrieve Your result from response
}
@Override
public void notifyError(String requestType,VolleyError error) {
Log.d("GJJJ","GJJJ1");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.