javaandroidhttp-postandroid-volleymalformedurlexception

Android Volley MalformedURLException: no protocol


Using Volley i am trying to send POST headers and receive a StringRequest but I'm getting a RuntimeException Caused by: java.net.MalformedURLException: no protocol, but i have a protocol.

I send a cookie and a token that are correct

Here is my code:

HttpClient httpClient = new DefaultHttpClient();
httpPost = new  HttpPost("http://ipServer:portServer/xxx/products.json?place_id=1");

        httpPost.addHeader("Content-type", "application/json");
        httpPost.addHeader("cookie", cookieTmp);
        httpPost.addHeader("X-CSRF-Token", tokenTmp);

StringRequest request = new StringRequest(Request.Method.GET, String.valueOf(httpPost),
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("VOLLEY", response);
                        startActivity(new Intent(getApplicationContext(), 
                        MainActivity.class));
                        LoginActivity.this.finish();
                }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                L.m("not response");
                handleVolleyError(error);

And im getting this exception:

E/Volley: [1070] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL org.apache.http.client.methods.HttpPost@c9461c0
      java.lang.RuntimeException: Bad URL org.apache.http.client.methods.HttpPost@c9461c0
          at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
          at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
       Caused by: java.net.MalformedURLException: no protocol: org.apache.http.client.methods.HttpPost@c9461c0
          at java.net.URL.<init>(URL.java:589)
          at java.net.URL.<init>(URL.java:486)
          at java.net.URL.<init>(URL.java:435)
          at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
          at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
          at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114) 

The tests I perform in Postman work correctly. Any ideas on what could be causing this?


Solution

  • In between of coding you used two different codes, such as "get" and "add".

    Please make it consistent.

    First try it with this:

     httpPost.addHeader("Content-type", "application/json"); 
     httpPost.addHeader("cookie", cookieTmp); 
     httpPost.addHeader("X-CSRF-Token", tokenTmp); 
    

    And then with this:

      httpPost.setHeader("Content-type", "application/json"); 
     httpPost.setHeader("cookie", cookieTmp); 
     httpPost.setHeader("X-CSRF-Token", tokenTmp); 
    

    Edited

    See error message on postman