【问题标题】:RequestQueue() in RequestQueue can not be appied [closed]无法应用RequestQueue中的RequestQueue()[关闭]
【发布时间】:2017-03-02 11:00:38
【问题描述】:

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

【问题讨论】:

  • 因为很明显 RequestQueue 构造函数不带这样的参数...
  • 那么我选择什么参数来实现来自我的服务器的简单请求。
  • 到目前为止你做了什么?请提供代码。
  • 请粘贴一些代码。 @MansiSharma
  • compile 'com.mcxiaoke.volley:library:1.0.19' 我正在为 android studio 2.2.3 使用这个依赖项

标签: android android-studio request android-volley


【解决方案1】:

对服务器的单个请求使用 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;
    }
}

希望这会有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多