How to resolve org.apache.http.conn.ConnectTimeoutException in Android? - java

I am working on service related application.
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000000);
HttpConnectionParams.setSoTimeout(params, 15000000);
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(),true);
String str_Server_Host = Control_PWD_Server_Name();
String requestEnvelope = String.format(envelope_oe);
// HttpPost httpPost = new HttpPost(str_Server_Host);
HttpPost httpPost = new HttpPost(str_Server_Host);
httpPost.setHeader("SOAPAction",
"http://tempuri.org/updateTransportStatus");
httpPost.setHeader("Content-Type", "text/xml;charset=utf-8");
httpPost.setEntity(new StringEntity(requestEnvelope));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
String str = Html.fromHtml(line).toString();
This is the exception I am getting:
org.apache.http.conn.ConnectTimeoutException: Connect to /10.64.0.184:9092 timed out
How to resolve above exception in android? please give any suggestion or related senario.

Related

Get List<byte[]> from InputStream

What I'm trying to do is to get List<byte[]> from InputStream. I'm sending HTTP request to an API that returns List<byte[]> and I want to get that list from the content body.
HttpPost httpPost = new HttpPost(endPoint);
CloseableHttpClient httpclient = HttpClients.createDefault();
httpPost.setEntity(new ByteArrayEntity(ReqBody));
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
System.out.println(response.getEntity().getContentType());
return response.getEntity().getContent();

Remove or replace special character in namevaluepair for http post request android

I have a problem when I tried to post my data it logged as :
[ObserverTRID=5QEET3TE10,
ObsType=Evaluate,
ObsDate=17-Jul-2014,
ObsTime=09:22:12,
// == loc
ObsTitle=bolllloooo,
//==obstset
MediaData={"MediaData":[{"RunNo":0,"Filename":"http://url/5QEET3TE10_3_5_20140717092206.jpg","MediaNo":4"Desc":01 Image_17-Jul-14No Marker.jpg}]},
PVPMediaCount=1,
SourceDevice=motorola XT1032 Android v19 App v2.0001,
ObsDept,
ObsSite,
TargetTRID=5QEET3TE10]
But I need to append every namevalue with "&" instead of "," This is my httppost request.
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
This is my request i am posting the URL with the parameter nameevaluepair
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(mUrl);
if(mNameValuePairs==null){
mNameValuePairs = new ArrayList<NameValuePair>(1);
mNameValuePairs.add(new BasicNameValuePair("PostEvent", "Null"));
}
Log.e("namevalue ", mNameValuePairs.toString());
There I am settig entity by URLEncoding
httppost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8);
HttpEntity httpEntity = response.getEntity();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
use this code in for parsing class

Adding parameter to HttpPost on Apache's httpclient

I am trying to set some Http parameters in the HttpPost object.
HttpPost post=new HttpPost(url);
HttpParams params=new BasicHttpParams();
params.setParameter("param", "value");
post.setParams(params);
HttpResponse response = client.execute(post);
It looks like the parameter is not set at all. Do you have any idea why this is happening?
Thank you
For those who hopes to find the answer using HttpGet, here's one (from https://stackoverflow.com/a/4660576/330867) :
StringBuilder requestUrl = new StringBuilder("your_url");
String querystring = URLEncodedUtils.format(params, "utf-8");
requestUrl.append("?");
requestUrl.append(querystring);
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet(requestUrl.toString());
NOTE: This doesn't take in consideration the state of your_url : if there is already some parameters, if it already contains a "?", etc. I assume you know how to code/search and will adapt regarding your case.
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("param", "value"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
httpClient.execute(httpPost);

java- unknown host exception when using apache http client

I am trying to make a simple GET request for a website, but I am getting unknown host exception.
Given below is my code--
DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost=null;
targetHost= new HttpHost("google.com/", 80, "http");
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localcontext = new BasicHttpContext();
try {
HttpResponse response = client.execute(targetHost, httpget, localcontext);
It looks like you have a simple problem here.
The URL for your 'HttpHost' object is malformed. You need to drop the '/' from "google.com/".
It should work after that. I used your code with that single modification & it worked.
DefaultHttpClient client = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("google.com", 80, "http");
HttpGet httpget = new HttpGet("about-us.html");
BasicHttpContext localContext = new BasicHttpContext();
HttpResponse response = null;
try { response = client.execute(targetHost, httpget, localContext);
System.out.println(response.getStatusLine()
}
catch(Exception e){
// Enter error-handling code here.
}

Exception: java.net.UnknownHostException:stage.provision.optimumwifi.com

I trying to make a API call using HttpPost.PFB the code
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://stage.provision.optimumwifi.com/services/AutoMacService/registerDevice");
httppost.addHeader("Authorization","Basic "+ Base64Coder.encodeString("********"));
StringEntity se = new StringEntity( xmlRequest, HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
The exception is -- java.net.UnknownHostException: stage.provision.optimumwifi.com

Categories