UnknownHostException in version 4.1.2 in Android - android

I have an HTTP get Service url and i can connect and get resource from this url on almost all version of sdk but in 4.1.2 it gives UnknownHostException. Internet permission is already there and also internet connection is active. Please help.
Here is code:
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("myhosturl");
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null)
{
String response = EntityUtils.toString(resEntityGet);
}
}
catch (Exception e)
{
e.printStackTrace();
}
Actually this url is called when there is any incoming call that time it gives exception. If i hit this url any other time it works.

Related

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate on HttpClient but works fine with HttpUrlConnection

Previously,I used HttpClient for a http post request and it was working fine, until I believe the server team made some changes. Then I kept getting
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate Exception.
Then, after alot of scratching my head, I tried HttpUrlConnection and it works fine, but still I can't figure out why I got that exception while using HttpClient.
Before code was :
public String postDataAndGetStringResponse( List<NameValuePair> nameValuePairs ) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost( link );
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
InputStream is = response.getEntity().getContent();
String result = "";
if (is != null) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String l = "";
while ((l = reader.readLine()) != null) {
result += l;
}
reader.close();
}
is.close();
return result;
} catch (Exception e) {
Logger.printStackTrace(e);
return ServerUnrechable;
}
}
I did check the server using https://www.sslshopper.com and everything is ticked, it would be very helpful if anybody could tell me the cause to this issue.
One of the most likely causes is that the server you're trying to use now relies on Server Name Indication.
SNI support was added a to HttpsURLConnection in Android, but not to the Apache HTTP Client bundled (now deprecated/removed). See this related question for details.

Android HTTPClient.execute stopped working after years

I have some Android applications that have not been touched in years. All of the sudden today they all stopped working. They all read data from a txt file that sits on an https:// site.
If I change the https:// call to http:// everything works fine again. I need the https:// for security. Can anyone tell me what happened?
Keep in mind that this application is a few years old. Did something change in an online library or something to break the https:// calls?
Here is the code. params[0] hold the website address. I have verified the address is correct. Just changing it to http:// fixes everything. II also know it is not the ssl certificate since I have verified it is all working.
It fails on the httpClient.execute command:
protected List<String> doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(params[0]);
HttpResponse response;
List<String> data = new ArrayList<String>();
try {
response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
InputStream is = buf.getContent();
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = r.readLine()) != null) {
data.add(line);
}
return data;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
I don't have any logcat or console output that specifies what the error actually is. I could not find anything.
Thanks for any help.

How to get Web content from web server to android application

I am doing a project in android, to get the website data's into my android application.
I tried JSON but it doesn't help me to achieve my project. Please help me...
i've used this code for my project
HttpClient client = new DefaultHttpClient();
String getURL = "http://yourservername/wifi.ashx?cmd=get;1;";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
String str;
if (resEntityGet != null) {
str = EntityUtils.toString(resEntityGet).substring(2, 4); // determined the maximumum id in server
str.lastIndexOf(str);
}
} catch (Exception e) {
e.printStackTrace();
// link.setText("errorororo");
}

Foursquare API 404 error: Endpoint not found

I am trying to do a GET request using the foursquare checkin endpoint. I'm getting back a 404 error which is endpoint not found. Any help with why that could be happening would be great!
try {
URI url = new URI("https://api.foursquare.com/v2/user/self/checkins?oauth_token="+TokenStore.get().getToken()+"&v=20140219");
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
is = entity.getContent();
int responceCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "Responce = "+ responceCode);
} catch(Exception e) {
e.printStackTrace();
}
The endpoint is users/self, not user/self, which you have. Just a typo :)

Socket closed exception when trying to read httpResponse

I have a method to connect to send post data to a webservice and get the response back as follow:
public HttpResponse sendXMLToURL(String url, String xml, String httpClientInstanceName) throws IOException {
HttpResponse response = null;
AndroidHttpClient httpClient = AndroidHttpClient.newInstance(httpClientInstanceName);
HttpPost post = new HttpPost(url);
StringEntity str = new StringEntity(xml);
str.setContentType("text/xml");
post.setEntity(str);
response = httpClient.execute(post);
if (post != null){
post.abort();
}
if (httpClient !=null){
httpClient.close();
}
return response;
}
Then, in my AsyncTask of my fragment, I try to read the response using getEntity():
HttpResponse response = xmlUtil.sendXMLToURL("url", dataXML, "getList");
//Check if the request was sent successfully
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// Parse result to check success
responseText = EntityUtils.toString(response.getEntity());
if (!xmlParser.checkForSuccess(responseText, getActivity())){
//If webservice response is error
///TODO: Error management
return false;
}
}
And when I reach that line:
responseText = EntityUtils.toString(response.getEntity());
I get an exception: java.net.SocketException: Socket closed.
This behavior doesn't happen all the time, maybe every other time.
Just write
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(your url);
HttpResponse response = client.execute(post);
it should work.No need to write codes which makes confusion.
I also experienced the 'socket closed' exception when using a client instance built using HttpClientBuilder. In my case, I was calling HttpRequestBase.releaseConnection() on my request object within a finally block before processing the response object (in a parent method). Flipping things around solved the issue... (working code below)
try {
HttpResponse response = httpClient.execute(request);
String responseBody = EntityUtils.toString(response.getEntity());
// Do something interesting with responseBody
} catch (IOException e) {
// Ah nuts...
} finally {
// release any connection resources used by the method
request.releaseConnection();
}

Categories

Resources