I am trying to implement ping using HttpGet but behavior is random.
I am having following code which test the internet/server connectivity:
boolean result = false;
HttpGet request = new HttpGet("www.MyServer.com");
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpClient = new DefaultHttpClient(httpParameters);
try
{
HttpConnectionParams.setConnectionTimeout(httpParameters, 6000);
HttpConnectionParams.setSoTimeout(httpParameters, 6000);
HttpResponse response = httpClient.execute(request);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK)
{
result = true;
}
}
catch(Exception e)
{
e.printStackTrace();
result = false;
}
Log.d("App", "Ping Result:"+result);
Above code I am running in thread as it may take time.
When I run this test for first time then I get result as true, but then after behavior is random, some times it given me error 'host unreachable' and I get result as false.
I just want to test is server is reachable from the currently configured Android network.
Is there any reliable API to test the internet/server connectivity?
UPDATE:
In a Service i have following function which initiates the test.
void startTest()
{
ServerTestThread mServerTestThread = new ServerTestThread()
mServerTestThread.start();
}
class ServerTestThread extends Thread
{
boolean result = false;
public void run()
{
//HttpGet code
//Send Message TO GUI Thread with result.
}
}
Above startTest function is creating instance of the test thread and calling start function. when test is done I am sending message to main thread which contains the result.
Thanks.
There is no problem with your code. That means either:
Sometimes server is really unreachable
Connection is slow and it time outs before server is reached.
So test setting timeout to some larger value, such as 60000(60sec), and check again. If it works, then you know it was because of timeout.
EDIT
Also please make this change, maybe it gives us more info:
Log.d("App", "Status:" + status);
if (status == HttpStatus.SC_OK)
{
result = true;
}
EDIT2
class ServerTestThread extends Thread
{
public static boolean result = false;
public static HttpGet request = new HttpGet("www.MyServer.com");
public static HttpParams httpParameters = new BasicHttpParams();
public static HttpClient httpClient = new DefaultHttpClient(httpParameters);
boolean result = false;
public void run()
{
//HttpGet code
//Send Message TO GUI Thread with result.
}
}
As a bonus,
this will tell if you if you're connected to a network.
Related
I'm developing a program that sends some Logs to a database.
As logs, the main idea is to save as much information as it can, I want to avoid the part if the server where I store logs is down. What I'm trying to do is to make an http request and totally ignore server response, it doesn't matter if is online or offline. What I'm done so far is to set a timeout to my request but that doesn't resolve my problem. Here is my code:
public synchronized void LOG_sendERROR(String token, String channelnumber, String type)
{
HttpResponse response = null;
HttpParams httpParameters = new BasicHttpParams();
int timeout = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeout);
int timeoutsocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutsocket);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
try
{
if (ep.getString("username", "").length() == 0)
return ;
String url = Server.Log_Server + "/LOG_CHANNEL_LOGS.aspx?params=1";
log.i("LOGS", url);
HttpGet c = new HttpGet(url);
response = httpclient.execute(c);
return ;
}
catch (Exception e)
{
e.printStackTrace();
try
{
if (response != null)
response.getEntity().consumeContent();
}
catch (Exception f)
{
}
return ;
}
}
If server is down my application stuck for 3 seconds. I want to send the logs, it doesn't matter for the client application to know whether the server saved the logs the client just send. How can I make a http request and ignore response?
After i tried many methods, the best way is to execute as a parallel task. In this way client wont ignore the response of the server but at least the application will be running normaly.
private Runnable LOG_CHANNEL_LOGS = new Runnable()
{
public void run()
{
new LOG_CHANNEL_LOGS().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
};
i'm stuck with a timeout problem with DefaultHttpClient on Android. I'm trying to set the timeout with the following piece of code:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 4000);
HttpConnectionParams.setSoTimeout(httpParameters, 4000);
But if the device is connected to a network without internet connection, the timeout is never fired and the execution of http request never throws any timeout exception. I'm executing http request as follow:
HttpResponse httpResponse = client.execute(request);
I've tried also to set the timeout on the HttpRequest, with the following lines:
HttpRequestBase request = ...
request.setParams(httpParameters);
Android seems to ignore the timeout settings and when executing http request on a network with no internet connection, all the requests fails after about 20s, and not after my timeout settings.
I've also tried to close all internet connections and abort http request after a timeout with a parallel thread. I've used the following piece of code:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpRequestBase request = ...
request.setParams(httpParameters);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutReal);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutReal);
request.setParams(httpParameters);
((DefaultHttpClient) client).setParams(httpParameters);
Thread t = new Thread(){
public void run()
{
try
{
Thread.sleep(4000);
request.abort();
client.getConnectionManager().closeExpiredConnections();
client.getConnectionManager().closeIdleConnections(4000,TimeUnit.MILLISECONDS);
client.getConnectionManager().shutdown();
Log.i("TEST SHUTDOWN","SHUT DOWN ALL CONNECTIONS");
}
catch (InterruptedException e)
{
}
}
};
try
{
t.start();
HttpResponse httpResponse = client.execute(request);
}
catch (Exception e)
{
Log.i("TEST SHUTDOWN","EXCEPTION "+e);
}
finally
{
t.interrupt();
}
But even if I see from the logs that the request is aborted and the connection manager is shut down, the execution of the request is not interrupted/aborted and no exception is raised at the timeout set.
The request ends always after 20s.
Any idea why?
What you are probably seeing is that the DNS lookup is timing out. When your HTTP client attempts to make a connection, the first thing it does is try to resolve the hostname of your URL into an IP address. When doing this it doesn't take your timeout settings into account (those timeout values are only used when trying to actually make the socket connection). If you haven't got good Internet connectivity, your DNS lookup will just stall until it times out. Once that happens, your HTTP request should fail immediately with UnknownHostException.
Unfortunately you have no control over the DNS timeout, so the only way to solve your problem is to first determine if your DNS resolution is working. You should do this in a separate thread, and if you don't get successful host resolution within a few seconds, you know that your Internet connection is not reliable and you don't even have to attempt your HTTP request.
So you can try something like this:
HttpClient client = new DefaultHttpClient();
HttpParams httpParameters = client.getParams();
HttpRequestBase request = ...
request.setParams(httpParameters);
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutReal);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutReal);
request.setParams(httpParameters);
((DefaultHttpClient) client).setParams(httpParameters);
// The thread that is waiting to execute the HTTP request
final Thread waitingThread = Thread.currentThread();
Thread t = new Thread() {
boolean running = true;
public void run() {
try {
// Try to resolve the host name
InetAddress addr = InetAddress.getByName (hostname);
// Successful resolution, notify the waiting thread
if (running) {
// Signal the waiting thread that it can do the HTTP request now
waitingThread.interrupt();
}
} catch (Exception e) {
// Some problem, just ignore it
}
}
};
try {
// Start name resolution
t.start();
// Sleep for as long as we are willing to wait for the DNS resolution
Thread.sleep(4000);
// If we slept the entire time without getting interrupted, the DNS resolution took too long
// so assume we have no connectivity.
t.running = false; // We don't want to be interrupted anymore
// Don't even bother trying the HTTP request now, we've used up all the time we have
} catch (InterruptedException ie) {
// We got interrupted, so the DNS resolution must have been successful. Do the HTTP request now
HttpResponse httpResponse = client.execute(request);
}
I'm writing this code without trying it, so please forgive any typos or missing semicolons. You should get the idea.
You could try to use AndroidHttpClient instead of DefaultHttpClient. It has some specific settings for Android.
Alternatively, you could try to replace the following line:
HttpParams httpParameters = client.getParams();
with the following line
HttpParams httpParameters = new BasicHttpParams();
I don't know if this is the correct answer but, hope this helps.
The 20 seconds part of your question rfc2616 specifies 5 redirects times 4000 milis = 20 Seconds. The solution is to check for network connection before attempting network access.
// check for network connection
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connMgr == null) {
return false; // I get a null here when there is no connection on my lg f6.
}
// check ok to process
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null || !networkInfo.isConnected()) {
return false;
}
return true;
Well, I had a similar problem. This is what I did to solve it.
First, I create an instance of the HttpParams:
HttpParams params = new BasicHttpParams();
Then, I don't set the settings manually. Instead I use the HttpConnectionParams class to do so. For example:
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, httpTimeout * 1000);
HttpConnectionParams.setSoTimeout(params, httpTimeout * 1000);
Then, I pass the created params when I instantiate the html client:
HttpClient client = new DefaultHttpClient(params);
I also use a Client Connection Manager, so, I pass it as the first parameter in the above call.
So, the trick is to create the HttpClient by passing already setted parameters and not chaging them afterwards.
Hope this works for you.
EDIT: Did you tried to block the redirects? HttpClientParams.setRedirecting(params, setRedirecting);
When my activity loads, I am connecting to a web service. As and when I get the response from service, I again call then service and so on.
#Override
protected void onCreate(Bundle savedInstanceState) {
….
callWebMethod();
}
// Called on getting response
#Override
public void run(String value) {
….
callWebMethod();
}
This is how I am connecting to service
HttpGet request = new HttpGet(url + combinedParams);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpResponse httpResponse;
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
response = StringUtils.remove(response, "\n");
response = StringUtils.remove(response, '"');
}
Is it possible that I connect to the service only once at the start, then the connection remains open and application keeps on reading data from service till connection is forcefully closed.
Please let me know if more code is required.
Update: I then tried with ClientConnectionManager but still connection is again and again initialising. Though it is getting data. What I want is that connection remains open, and keeps on reading data from service.
HttpParams httpParameters = new BasicHttpParams();
SharedPreferences preferences = context.getSharedPreferences(
"MyPreferences", Context.MODE_PRIVATE);
int timeoutConnection = Integer.parseInt(preferences.getString(
"timeout", "60")) * 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, 2000);
System.setProperty("http.keepAlive", "true");
HttpClient client = new DefaultHttpClient(httpParameters);
ClientConnectionManager mgr = client.getConnectionManager();
client = new DefaultHttpClient(new ThreadSafeClientConnManager(
client.getParams(), mgr.getSchemeRegistry()),
client.getParams());
while (true) {
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
response = StringUtils.remove(response, "\n");
response = StringUtils.remove(response, '"');
((Activity) context).runOnUiThread(new Runnable() {
public void run() {
callback.run(response); // This calls activity callback function.
}
});
// Closing the input stream will trigger connection release
// instream.close();
}
} catch (ConnectTimeoutException e) {
….
}
It sounds like what you really need is a socket connection (see here). A socket will stay connected and allow you to stream data back and forth with the socket server until you are finished.
you just need to close the InputStream you get from HttpResponse.getEntity().getContent() after you are done using/reading-it. This will officially indicate the end of your current request.
You can then proceed to execute another request, the same HttpClient connection will be used.
Add a close
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// close the InputSream
instream.close()
// you can now reuse the same `HttpClient` and execute another request
// using same connection
httpResponse = client.execute(request);
Is it possible that I connect to the service only once at the start,
then the connection remains open...
The web server has a role to play in this. If the server "ends" the HTTP response, there is no further communication going to happen on same HTTP call.
It is possible to keep an HTTP connection open, with help of server. In this case, server never really ends the response but keeps writing data to response stream after some time intervals, so client can keep listening.
The new replacement for the above technique is a duplex socket connection. Both client and server can send and receive messages over a socket. Again, both client and server have to support it properly, and necessary handling for connection drops etc has to be there.
There are android specific client implementations available like https://github.com/nkzawa/socket.io-client.java that take care of most of connection management for you.
I think you could try to use the AsyncTask class to try to keep your thread open and do what you want, like this:
public class ConnectToWebService extends AsyncTask<Void, Void, Boolean> {
#Override
protected Boolean doInBackground(Void... params) { ... }
#Override
protected void onPostExecute(final Boolean success) { ... }
#Override
protected void onCancelled() { ... }
}
Check the API documentation for more information ;)
I'm having a real hard time figuring out how to parse a none standard HTTP response.
The none standard response contains ICY 200 OK instead of HTTP 200 OK. Here is a sample URL that sends the none standard HTTP response.
http://50.117.121.162:80
Since Android 4.4 HttpURLConnection will no longer work with these none standard responses. I have tried using the HttpClient by Apache but it doesn't work because of the none standard HTTP response. I have then tried following the guide for adding a custom response parser, but Android doesn't seem have all the classes needed to do it.
I'm really struggling to figure out a solution. Possibly modify the none standard response before it is parsed by the HttpClient or the HttpURLConnection could work but I'm not sure if that is even possible...
Any help would be greatly appreciated.
After a lot of research for a small/lite http client library, I ran into this port of the apache httpclient for android. The library provided a complete support for http connections. Then I simply modified the source code, particularly the BasicLineParser to replace ICY with HTTP/1.0.
I had similar problem with KitKat and had a success with using two classes found here for http post. They are incredibly easy to use and you can modify the protocol params easily too.
There is another solution to this issue in Android 4.4 but it requires using Apache HttpClient. This is based on possibility of providing custom response parser into Apache Http engine that can change ICY 200 OK to HTTP/1.0 200 OK. This is based on general idea presented in:
http://hc.apache.org/httpcomponents-client-4.2.x/tutorial/html/advanced.html
I have successfully used following code.
public class IcyClientConnection extends DefaultClientConnection{
#Override
protected HttpMessageParser createResponseParser(SessionInputBuffer buffer,
HttpResponseFactory responseFactory, HttpParams params) {
return new IcyHttpResponseParser(
buffer,
new BasicLineParser (),
responseFactory,
params);
}
}
public class IcyClientConnectionOperator extends DefaultClientConnectionOperator {
public IcyClientConnectionOperator(SchemeRegistry schemes) {
super(schemes);
}
#Override
public OperatedClientConnection createConnection() {
return new IcyClientConnection();
}
}
public class IcyClientConnManager extends SingleClientConnManager {
public IcyClientConnManager(HttpParams params, SchemeRegistry schreg) {
super(params, schreg);
}
#Override
protected ClientConnectionOperator createConnectionOperator(
SchemeRegistry schreg) {
return new IcyClientConnectionOperator(schreg);
}
}
Now you have to extend parser used by default and add code that will change wrong server replay to correct one. Normally code will block on hasProtocolVersion.
public class IcyHttpResponseParser extends DefaultResponseParser{
private CharArrayBuffer icyLineBuf;
private int icyMaxGarbageLines = 1000;
private final HttpResponseFactory icyResponseFactory;
public IcyHttpResponseParser(SessionInputBuffer buffer, LineParser parser,
HttpResponseFactory responseFactory, HttpParams params) {
super(buffer, parser, responseFactory, params);
this.icyLineBuf = new CharArrayBuffer(128);
icyResponseFactory = responseFactory;
}
#Override
protected HttpMessage parseHead(SessionInputBuffer sessionBuffer)
throws IOException, HttpException {
int count = 0;
ParserCursor cursor = null;
do {
// clear the buffer
this.icyLineBuf.clear();
final int i = sessionBuffer.readLine(this.icyLineBuf);
//look for ICY and change to HTTP to provide compatibility with non standard shoutcast servers
String tmp = icyLineBuf.substring(0, this.icyLineBuf.length());
if(tmp.contains("ICY ")){
tmp = tmp.replace("ICY", "HTTP/1.0");
}
//copy
this.icyLineBuf = new CharArrayBuffer(128);
System.arraycopy(tmp.toCharArray(), 0, icyLineBuf.buffer(), 0, tmp.length());
icyLineBuf.setLength( tmp.length());
if (i == -1 && count == 0) {
// The server just dropped connection on us
throw new NoHttpResponseException("The target server failed to respond");
}
cursor = new ParserCursor(0, this.icyLineBuf.length());
if (lineParser.hasProtocolVersion(this.icyLineBuf, cursor)) {
// Got one
break;
} else if (i == -1 || count >= this.icyMaxGarbageLines) {
// Giving up
throw new ProtocolException("The server failed to respond with a " +
"valid HTTP response");
}
//if (this.log.isDebugEnabled()) {
// this.log.debug("Garbage in response: " + this.lineBuf.toString());
// }
count++;
} while(true);
//create the status line from the status string
final StatusLine statusline = lineParser.parseStatusLine(this.icyLineBuf, cursor);
return this.icyResponseFactory.newHttpResponse(statusline, null);
}
}
Plug in HttpClient:
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80);
Scheme ftp = new Scheme("ftp", PlainSocketFactory.getSocketFactory(), 21);
SchemeRegistry sr = new SchemeRegistry();
sr.register(http);
sr.register(ftp);
HttpClient httpClient = new DefaultHttpClient(new IcyClientConnManager(params, sr), params);
This is still being tested but initial results are promising.
Thanks #Michael M, you can even make it simpler by subclassing the BasicLineParser instead of subclassing the DefaultResponseParser.
I've uploaded the code into a gist
To use it:
IcyGetRequest request = new IcyGetRequest(urlStr);
HttpResponse response = request.get();
int responseCode = response.getStatusLine().getStatusCode();
Create an runnable that creates socket proxy then you will be able to response with HTTP/1.0 instead of ICY , then just connect to this local socket proxy with your player
Here a modification of the solution from Michal M in case you don't like to create lots of subclasses just to configure already available HttpClient classes.
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
HttpClient httpClient = new DefaultHttpClient() {
#Override
protected ClientConnectionManager createClientConnectionManager() {
return new SingleClientConnManager(getParams(), schemeRegistry) {
#Override
protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg) {
return new DefaultClientConnectionOperator(schreg) {
#Override
public OperatedClientConnection createConnection() {
return new DefaultClientConnection() {
#Override
protected HttpMessageParser createResponseParser(SessionInputBuffer buffer, HttpResponseFactory responseFactory, HttpParams params) {
return new IcyHttpResponseParser(buffer, new BasicLineParser(), responseFactory, params);
}
};
}
};
}
};
}
};
Probably there is a way to get the SchemeRegistry obsoleted if one could get hold somehow from within the DefaultHttpClient class.
I am currently faced to a strange problem.
I have to contact a web service, with a very long URL (there is some XML inside). The length of one of them is 943 characters.
Most of the time, the request failed with a NoHttpResponseException.
I newly added a RetryHandler, which do his job, and the request finally worked, but the execute time was 246 seconds!
I reduced the timeout, to something like 30 seconds, and occasionally, the request work.
Is there something to know about long URL to make it work better?
Or, is it just prohibited on Android?
I precise that all connection with another tinier URL (even like 200 chars) perfectly work.
Here the source code of the Http connection:
DefaultHttpClient hc = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(hc.getParams(), false);
HttpParams httpParameters = hc.getParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
hc.setParams(httpParameters);
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
// retry a max of x times
if(executionCount >= 5){
return false;
}
if(exception instanceof NoHttpResponseException){
return true;
} else if (exception instanceof ClientProtocolException){
return true;
}
return false;
}
};
hc.setHttpRequestRetryHandler(retryHandler);
url = Tool.prepareURL(url);
Log.d(LogFilter.EXECUTE, url);
HttpGet get = new HttpGet(url);
if (eTag != null) {
get.addHeader(HEADER_IF_NONE_MATCH, eTag);
}
long time = System.currentTimeMillis();
HttpResponse rp = hc.execute(get);
Log.d(LogFilter.EXECUTE, "temps execute: "+(System.currentTimeMillis()-time));
return rp;
Thank you for your time.
I think this is a server side problem and may not respond (much) to setting the timeouts. Have you tried pasting the long url into a browser?