在Volley JSONArray请求中传递参数

时间:2016-05-24 11:23:41

标签: android json android-volley

我正在尝试根据参数即电子邮件地址从服务器获取帖子。如何在JSONArrayRequest中发送参数,以便只检索与给定参数匹配的那些帖子?这是我的代码: 的 JsonArrayRequest:

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            for(int i= 0; i<response.length(); i++){
                try {
                    JSONObject object = response.getJSONObject(i);
                    Post post = new Post();

                    post.setContent(object.getString("Content"));
                    post.setDate(object.getString("PostDate"));
                    post.setTime(object.getString("PostTime"));

                    postArray.add(post);
                    PostList.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    AppController.getmInstance().addToRequestQueue(jsonArrayRequest);

这是 AppController.Java 类:

public class AppController extends Application {
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
public static final String TAG = AppController.class.getSimpleName();

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getmInstance() {
    return mInstance;
}

public RequestQueue getmRequestQueue()
{
    if(mRequestQueue == null){
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}
public ImageLoader getmImageLoader(){
    getmRequestQueue();
    if(mImageLoader == null){
        mImageLoader = new ImageLoader(this.mRequestQueue, new BitmapCache());
    }
    return mImageLoader;
}

public <T> void addToRequestQueue(Request<T> request, String tag ){
    request.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getmRequestQueue(). add(request);

}

public <T> void addToRequestQueue(Request<T> request ){
    request.setTag(TAG);
    getmRequestQueue(). add(request);

}

public void cancelPendingRequest(Object tag){
    if(mRequestQueue != null){
        mRequestQueue.cancelAll(tag);
    }
}

}

1 个答案:

答案 0 :(得分:2)

      void MakePostRequest() {
            StringRequest postRequest = new StringRequest(Request.Method.POST, EndPoints.BASE_URL_ADS,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonResponse = new JSONObject(response);
                                value1= jsonResponse.getString("Your ID1");
                                value2= jsonResponse.getString("Your ID2");

                            } catch (JSONException e) {
                                e.printStackTrace();
                                banner_id = null;
                                full_id = null;
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                            value1= null;
                            value2= null;
                        }
                    }
            ) {
           // here is params will add to your url using post method
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("app", getString(R.string.app_name));
                    //params.put("2ndParamName","valueoF2ndParam");
                    return params;
                }
            };
            Volley.newRequestQueue(this).add(postRequest);
        }

此帖子请求正在使用此compile 'com.mcxiaoke.volley:library:1.0.19'排球版。

我只是添加应用名称作为参数。您可以添加更多参数。