Android - Error in http connection java.net.UnknownHostException: - android

Im trying to connect mysql with android but it throw me an error.
Error in http connection java.net.UnknownHostException:
Below is my code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.pherma.net84.net/admin/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
But the URL s working fine. Please copy n paste the url.

I forget to put network permission in manifest. Working fine now.

Related

I am making a simple login check application

when i am trying to run my application through an android device i gets the error as
Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.105 refused.
I am using same network for my computer and device.
in my mainactivity i have
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://"+ip+"//Zeditsho_app/login.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
and my ipconfig activity is
public class IpconfigActivity {
public static String ip="192.168.0.105";
}
could anyone help me to sort out this?

Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to <ip address> refused

I have implemented one Android application, which contains remote connection.
My code is as follow: I am testing my app in my mobile device but I am getting error like:
Error in http connection org.apache.http.conn.HttpHostConnectException: Connection to 127.0.0.1 refused
I have tried 127.0.0.1, 10.0.2.2 and my pc's ip address too, but nothing is working out.
String url = "http://127.0.0.1/test.php";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}

Android HTTP PUT request probably differs from Post, because it doesnt work?

Here is how I use POST and it works:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
And I didnt know how to use PUT, so I got this code and I changed every "Post" to "Put"
but I dont think it works:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPut);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
When I try updating a user's account using this implementation ot PUT I get errors from the server. - "No session. Unauthorized."
When I use Chrome's postman with the same parameters, I get no problem, so I think my PUT implementation doesnt work.
You need to use the same DefaultHttpClient to make all the calls, because your session information is stored in the instance object.
If you need to use different instances, you may be able to do that getting the cookies from the login request and add them in the next requests using getCookieStore/setCookieStore.

MultipartEntity not creating good request

I needed to send some XML to a webservices and I was able to do it with a normal StringEntity because it was just text but now I need to attach an image to it as well. I tried doing it with a MultipartEntity but I couldn't get it working with just the XML.
// Working
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");
HttpEntity entity = new StringEntity(writer.toString());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
// not working
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost doc = new HttpPost("http://mywebservices.com");
// no difference when removing the BROWSER_COMPATIBLE
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("xml", new StringBody(writer.toString()));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity responseEntity = response.getEntity();
And is there a way I could see the MIME that is being send?
You simply forgot:
httppost.setEntity(entity);
By the way, it's probably good form to set the Content-Type of your parts, e.g.:
entity.addPart("xml", new StringBody(writer.toString(),"application/xml",Charset.forName("UTF-8")));
As far as seeing what's being sent, see http://hc.apache.org/httpcomponents-client-ga/logging.html (especially "wire logging") for the HttpClient logging features, and
this question for how to get it working on Android.
Another way to see what's being sent is to set up your own "server" to receive the request. You can do this on a Unix-like system with netcat. The command-line
nc -l 1234
starts a server listening on port 1234, and will echo whatever is received.
If that "server" is on a machine 10.1.2.3, you can just use a new HttpPost("http://10.1.2.3:1234") to send the message there.
I have a similar problem but I'm sending the multipart with user/pass to a acegi security system, it works with this:
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
But not with this:
try {
for (NameValuePair param : params) {
multientity.addPart(param.getName(), new StringBody(param.getValue(), Charset.forName(encoding))));
}
request.setEntity(multientity);
}

Logcat printing "interface name: null" during each DefaultHttpClient execution method call

I am contacting a web service multiple times to get a JSON String via HttpGet and DefaultHttpClient.
...
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = (HttpResponse)defaultHttpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
...
I find that LogCat is printing interface name: null with tag System.out for each time HttpResponse httpResponse = (HttpResponse)defaultHttpClient.execute(httpGet); is executed.
Am i establishing this http connection properly with HttpGet? Is there a different way?
How can i create this connection and not get interface name: null LogCat messages from the System.out tag?
Are you using the DefaultHTTPClient Asynchronously? I had a similar error in the past (Android 2.x) and found once I made a singleton for the DefaultHTTPClient and put my web request inside an AsyncTask this error went away.
Use these;
HttpClient client = new DefaultHttpClient();

Categories

Resources