【问题标题】:Get Request JsonArray with volley使用 volley 获取请求 JsonArray
【发布时间】:2017-05-24 19:02:01
【问题描述】:

我按照this 问题在不同的活动中重用凌空服务,但他只是使用 JSONOBject 来获取和发布请求,我需要返回一个 JSONArray,因为我的请求返回了超过 1 个项目。

所以我的 Volley Service 上有这样的内容:`public void

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

问题是我的响应(volleyService)的第二个参数有错误,说它需要一个 JsonObject。

我的 IResult 想要一个 JSONObject 而不是 JSONArray

【问题讨论】:

  • 你能在这里发表你的回应吗?最好了解一下您的回复会是什么样子。

标签: java android http android-studio android-volley


【解决方案1】:

像这样修改你的界面

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

【讨论】:

  • 很好的答案,但我只是在服务方面得到我的回应,而不是在我的活动中你知道为什么吗?
  • 无法理解您的问题。你能详细说明一下吗?我的回答对你没有帮助吗?
  • 我现在解决了这个问题,我只想要 2 个听众,但界面希望我总是创建 3 个,非常感谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多