RequestQueue中的RequestQueue()无法应用

时间:2017-03-02 11:00:38

标签: android android-studio request android-volley

[这是我的xml文件] [1]] 1我从服务器创建了简单的请求。但它显示了一些错误,如给定的图片所示。请帮我删除此错误。

this is my MainActivity class file

1 个答案:

答案 0 :(得分:0)

对服务器的单个请求使用StringRequest:

  public void onClick(View view){

      StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url
                , new Response.Listener<String>() {
      @Override
      public void onResponse(String response) {

          //Do your stuff on response

      }, new Response.ErrorListener() {

      @Override
      public void onErrorResponse(VolleyError volleyError) {

          //Do your stuff on Error response

        });      
      VolleyHelper.getInstance(getApplicationContext()).getRequestQueue().add(stringRequest);
}

这是你的VolleyHelper课程:

public class VolleyHelper {

    public static VolleyHelper mSingleton;
    private RequestQueue mQueue;
    private static Context mContext;

    private VolleyHelper(Context ctx) {
        mContext = ctx;
        mQueue = getRequestQueue();
    }

    public RequestQueue getRequestQueue() {
        if (mQueue == null) {
            mQueue = Volley.newRequestQueue(mContext);
        }
        return mQueue;
    }

    public synchronized static VolleyHelper getInstance(Context ctx) {
        if (mSingleton == null) {
            mSingleton = new VolleyHelper(ctx);
        }
        return mSingleton;
    }
}

希望这会有所帮助。 :)