How to solve the error: null in the volleyball library?

advertisements

In my project I am using volley library to get data(text and image) from server and show the data in RecyclerView. The problem is all the request(POST) I am sending are give no response and error : null(error.getMessage() is null). I have searched about it for two days but unable find any solution.

This is my volley singleton class :

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

}

I am using this code to send request :

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

I am unable find what is the problem. Please help me with this or there is any other library which I can use for http request please let me know. I need to finish this as soon as possible. Thanks in advance.


try to add getHeaders() method with request and check,,,, its just a hit and trial...

it may be the issue that on request the server wouldn't find the request content-type

.. try

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