Android Volley MalformedURLException:没有协议

时间:2017-11-29 22:21:47

标签: java android http-post android-volley malformedurlexception

使用Volley我正在尝试发送POST标头并收到一个StringRequest,但我得到一个RuntimeException引起:java.net.MalformedURLException:没有协议,但我有一个协议。

我发送了一个正确的cookie和令牌

这是我的代码:

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

我得到了这个例外:

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) 

我在Postman中执行的测试工作正常。 关于可能导致这种情况的任何想法?

1 个答案:

答案 0 :(得分:0)

在编码之间你使用了两个不同的代码,例如“get”和“add”。

请保持一致。

首先尝试一下:

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

然后用这个:

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

被修改

See error message on postman