Reputation: 1203
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)
Upvotes: 0
Views: 960
Reputation: 81
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();
Upvotes: -1
Reputation: 353
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.
Upvotes: 1
Reputation: 1571
http is not recomemended to send requests you can change it to volley.
see this link:
and if you need help you can try search here or in the web.
Upvotes: 1