如何解决错误:凌空库中的null?

时间:2015-08-08 11:58:26

标签: android httprequest android-recyclerview android-volley

在我的项目中,我使用volley库从服务器获取数据(文本和图像)并在RecyclerView中显示数据。问题是我发送的所有请求(POST)都没有给出响应和错误:null(error.getMessage()为null)。我已经搜索了两天但找不到任何解决方案。

这是我的凌空单身人士课程:

import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class HttpController extends Application {

public static final String TAG = HttpController.class
        .getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

private static HttpController mInstance;

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

public static synchronized HttpController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        mImageLoader = new ImageLoader(this.mRequestQueue,
                new LruBitmapCache());
    }
    return this.mImageLoader;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    // set the default tag if tag is empty
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

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

}

我正在使用此代码发送请求:

StringRequest data = new StringRequest(Request.Method.POST,
                "http://odishasamaya.com/news/api/get_category_posts/", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "News request Response : " + response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getString("status").equalsIgnoreCase("ok"))
                        adapter.setNewsList(getNewsData(jsonObject.getJSONArray("posts"), progressBar, news));

                    else
                        Log.e("tabs name", "array is null");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Request Error: " + error.getMessage());
                Toast.makeText(getActivity(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();

                params.put("id", id.get(getArguments().getInt("position")));
                Log.d("params are :", "" + params);

                return params;

            }
        };
        //HttpController.getInstance().cancelPendingRequests(tag_json_req);
        HttpController.getInstance().addToRequestQueue(data, tag_json_req);

我无法找到问题所在。请帮我这个或有任何其他库我可以用于http请求请告诉我。我需要尽快完成这项工作。提前谢谢。

3 个答案:

答案 0 :(得分:1)

尝试添加带请求的getHeaders()方法并检查,,,,它只是一个命中和试用......

  

可能是根据要求服务器无法找到的问题   请求内容类型

.. 尝试

pulic void doRequest(){
StringRequest data = new StringRequest(Request.Method.POST,
        "http://odishasamaya.com/news/api/get_category_posts/", new      Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d(TAG, "News request Response : " + response);
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.getString("status").equalsIgnoreCase("ok"))
                adapter.setNewsList(getNewsData(jsonObject.getJSONArray("posts"), progressBar, news));

            else
                Log.e("tabs name", "array is null");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Request Error: " + error.getMessage());
        Toast.makeText(getActivity(),
                error.getMessage(), Toast.LENGTH_LONG).show();
        if(error.getMessage()==null)
        doRequest();
    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {

        Map<String, String> params = new HashMap<>();

        params.put("id", id.get(getArguments().getInt("position")));
        Log.d("params are :", "" + params);

        return params;

    }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            params.put("Content-Type","application/x-www-form-urlencoded");
            return params;
        }
};
//HttpController.getInstance().cancelPendingRequests(tag_json_req);
HttpController.getInstance().addToRequestQueue(data, tag_json_req);
}

答案 1 :(得分:1)

我遇到了同样的错误,经过一些试验和错误,在调用队列之前我不允许缓存,添加以下行:

<强> data.setShouldCache(假);

HttpController.getInstance()。addToRequestQueue(data,tag_json_req);

因此队列以空/不工作缓存开始。工作就像一个魅力。

答案 2 :(得分:0)

我找到了解决这个问题的方法。如果其他人将来需要它,我会在这里发帖。

pulic void doRequest(){
StringRequest data = new StringRequest(Request.Method.POST,
            "http://odishasamaya.com/news/api/get_category_posts/", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "News request Response : " + response);
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (jsonObject.getString("status").equalsIgnoreCase("ok"))
                    adapter.setNewsList(getNewsData(jsonObject.getJSONArray("posts"), progressBar, news));

                else
                    Log.e("tabs name", "array is null");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Request Error: " + error.getMessage());
            Toast.makeText(getActivity(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            if(error.getMessage()==null)
            doRequest();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> params = new HashMap<>();

            params.put("id", id.get(getArguments().getInt("position")));
            Log.d("params are :", "" + params);

            return params;

        }
    };
    //HttpController.getInstance().cancelPendingRequests(tag_json_req);
    HttpController.getInstance().addToRequestQueue(data, tag_json_req);
    }

在上面的代码中,我创建了一个添加请求的方法,在错误响应中我正在检查error = null然后再次添加请求。

我知道这不是正确的方法,但使用此代码应用程序不会崩溃,但需要一次又一次请求,直到请求不成功为止。

如果有人找到了更好的解决方案,请在此处发布。 谢谢:)