简体   繁体   English

截击获取请求JsonArray

[英]Get Request JsonArray with volley

I followed this question to reuse volley service across diferent acitivies, but he just use the JSONOBject to GET and POST request, i need to return a JSONArray because i return more then 1 items with my request. 我遵循了这个问题,可以在不同的活动之间重复使用截击服务,但是他只是将JSONOBject用于GET和POST请求,因此我需要返回JSONArray,因为我的请求会返回1项以上的内容。

So i have something like this on my Volley Service: `public void 所以我在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");
    }
}`

So i use this service on my main activity like this: 所以我在这样的主要活动上使用此服务:

Initialize 初始化

        initPlants();
    Log.d("RESULTCALL",mResultCallback.toString());

    mVolleyService = new VolleyService(mResultCallback,this);

    mVolleyService.getDataVolley(GETREQUEST,URL);

callback 打回来

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();
    }

The problem is i get a error in the second parameter on my response(volleyService), saying that it requires a JsonObject. 问题是我在response(volleyService)的第二个参数中收到错误,说它需要一个JsonObject。

My IResult want a JSONObject and not a JSONArray 我的IResult需要JSONObject而不是JSONArray

modify your interface like this 像这样修改您的界面

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

now come to your activity from where You call your volley. 现在,从您称为抽射的地方开始您的活动。 from "onCreate()" remove the "initPlants();" 从“ onCreate()”中删除“ initPlants();” method. 方法。 Write Your activity name like this 这样写你的活动名称

public class Your_Activity extends Activity implements IResult {

now Implement the override methods like 现在实现类似的覆盖方法

@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM