0

I was trying to send HTTP request from Android with using the pieces of codes I found in SO but I ve got two problems. First one is how can I put a header to my http request. Second one is when I try to execute it(httpclient.execute) I get the following error. I already tried the url with advanced rest client and got a valid response.

Here is a successfull request

POST /api/v1/login/ HTTP/1.1
HOST: api.example.com
user-agent: Example
content-type: application/json
content-length: 47

{"username":"test", "password":"123456"}

Here is my code.

String restUrl="https://api.example.com/api/v1/login/";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(restUrl);

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", mJidView.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("password",mPasswordView.getText().toString() ));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

I get error in httpclient.execute(httppost) Here is the additional logcat.

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
                      at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
                      at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
                      at java.net.InetAddress.getAllByName(InetAddress.java:752)
                      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
                      at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
                      at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
                      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
Prethia
  • 1,183
  • 3
  • 11
  • 26

3 Answers3

1

http is not recomemended to send requests you can change it to volley.

see this link:

volley Google developers

and if you need help you can try search here or in the web.

Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17
  • I will also look at that but can you identify the problem in here. There are other people in SO who did it like this but my program keeps giving this error – Prethia Dec 18 '16 at 21:25
  • probably because Google deprecated http request in android. see this link to better understand [link](http://stackoverflow.com/questions/29294479/android-deprecated-apache-module-httpclient-httpresponse-etc) – Bruno Ferreira Dec 18 '16 at 21:32
1

Probably you got StrictMode enabled and you make your http request in main thread which is not permitted in Android right now.

Please make your request in working thread, or change StrictMode policy:

Error StrictMode$AndroidBlockGuardPolicy.onNetwork

But generally - you should make request in working thread.

Community
  • 1
  • 1
Marcin W
  • 353
  • 4
  • 7
  • Yeah it worked when I changed the strict policy. This answer is correct. I will accept it after finding how to put headers – Prethia Dec 18 '16 at 22:14
-1

Have you tried setting a header for your Post request? like this

 httpPost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
 httpPost.setHeader("Accept", "application/json");
 httpPost.setHeader("Accept-Charset", "utf-8");
 httpPost.setEntity(new UrlEncodedFormEntity(NameValuePairs, "UTF-8"));

Edit:

String restUrl="https://api.example.com/api/v1/login/";

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(restUrl);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", mJidView.getText().toString()));
        nameValuePairs.add(new     BasicNameValuePair("password",mPasswordView.getText().toString() ));
        httpPost.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Accept-Charset", "utf-8");

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        }catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
AndroidNewbie
  • 81
  • 1
  • 4
  • One question. What does the link represent in addHeader(content type)?Because I already created a httpPost with giving my Url to its constructor. Can you format your answer according to my code – Prethia Dec 18 '16 at 22:22
  • @AndroidNewbie does `{"username":"test", "password":"123456"}` looks like `x-www-form-urlencoded` for you? – Selvin Dec 19 '16 at 10:12