Android code to send http request to a local server - android

I have implemented HttpUrlConnection to send a request to a local http server (common LAN,Wifi,WifiAP). The code works perfectly fine for normal web servers (www.xyz.com), but fails to do so for local servers (192.168.x.y:z).
The code:
1) The function which makes the request
public static String getDef(String urlInput) {
URL url = null;
try {
url = new URL("http://" + urlInput);
Log.d("http", "calling " + url.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "close");
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.connect();
InputStream in = null;
in = (conn.getInputStream());
String response =readIt(in,5) ;
//response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
return response;
} catch (IOException e) {
Log.e("http","Error");
e.printStackTrace();
}
return "LocalClient didnt work";
}
2) The function which calls the above function.
new AsyncTask<String, Void, String>(){
#Override
protected String doInBackground(String... params){
try {
return LocalClient.getDef(URL);
} catch (Exception e){
e.printStackTrace();
}
return "Didnt work";
}
#Override
protected void onPostExecute(String string) {
message.append(string);
}
}.execute();
3)The request is received at the server with the following headers.
GET / HTTP/1.1
Connection: close
User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1.1; Nexus 5 Build/LYZ28E)
Host: 192.168.0.101
Accept-Encoding: gzip
It would be wonderful if someone could point me in the right direction.
I have also tried OkHttp,
public static String get(String url) throws IOException {
OkHttpClient client = new OkHttpClient();
try {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
resp= response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return resp;
}
On using a normal webserver the response is properly returned (source) but while using a local server I get the following error
java.io.EOFException
W/System.err﹕ at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:95)
W/System.err﹕ at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:179)
W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:628)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
W/System.err﹕ at com.demo.demo.http.LocalClient.getDef(LocalClient.java:56)
W/System.err﹕ at com.demo.demo.wifi.connector$2$2.doInBackground(connector.java:109)
W/System.err﹕ at com.demo.demo.wifi.connector$2$2.doInBackground(connector.java:105)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

The problem was server-side and was resolved by using appropriate headers.
OkHttp still doesn't seem to work and the function "public static String getDef(String urlInput)" mentioned above works flawlessly.

It seems there's an open bug about this issue:
https://github.com/square/okhttp/issues/1518
They suggest adding android:vmSafeMode="true" to the manifest file.

Related

Transfer data from one device to another - android

so I have created two separate apps, one acts as a client & the other acts as a server. Both devices are connected using hotspot:
Connection between the devices is successful. For transferring data from the server to the client, I am using the below code:
public static String downloadDataFromSender(String apiUrl) throws IOException {
InputStream is = null;
try {
URL url = new URL(apiUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
/* Starts the query */
conn.connect();
conn.getResponseCode();
is = conn.getInputStream();
/* Convert the InputStream into a string */
return readIt(is);
} finally {
if (is != null) {
is.close();
}
}
}
But I keep getting the following error:
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:343)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:205)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:187)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
at java.net.Socket.connect(Socket.java:586)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:113)
at com.android.okhttp.Connection.connectSocket(Connection.java:196)
at com.android.okhttp.Connection.connect(Connection.java:172)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at com.wifiscanner.utils.DownloadUtils.downloadDataFromSender(DownloadUtils.java:27)
at com.wifiscanner.tasks.SenderAPITask.doInBackground(SenderAPITask.java:29)
at com.wifiscanner.tasks.SenderAPITask.doInBackground(SenderAPITask.java:14)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
The url I am using in the download is this:http://192.168.43.1:52287/files
The connection is successful as I said. So not sure why I am getting network is unreachable. This happens intermittently and not always.
Could someone provide some detail on why this is happening?

Https url is not working but http url is working fine in android

Please guide me to find the problem in this code, when i use url with HTTP then it works fine but with HTTPS it's giving problem.I have googled but couldn't find a solution. It would be appreciable if someone guide me to save my time Thanks
public String doRequest() {
String serverUrl = Constants.BASE_URL + mAction;
Log.d("usm_serverUrl",serverUrl);
if (mParams != null) {
serverUrl += "?" + mParams;
}
Log.d("usm_serverUrl",serverUrl);
try {
URL url = new URL(serverUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent", "application/json");
conn.setRequestMethod("GET");
if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
// Do normal input or output stream reading
Log.d("usm_requestCode","Successful");
}
else {
Log.d("usm_requestCode","Failed");
// response = "FAILED"; // See documentation for more info on response handling
}
//HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(conn.getInputStream());
Log.d("debug", "get-url: " + serverUrl);
String response = IOUtils.toString(in, "UTF-8");
Log.d("debug", "get-url: " + serverUrl + "\n post-response: " + response);
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
I'm getting the following exception:
11-30 22:43:30.553 5031-5398/com.dhcollator.routetoschool W/System.err﹕ javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.Connection.connect(Connection.java:107)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
11-30 22:43:30.554 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
11-30 22:43:30.555 5031-5398/com.dhcollator.routetoschool W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
For https url connection to work in android, use HttpsUrlConnection instead of HttpUrlConnection.
See Documentation here
refer link, it will help as HttpsUrlConnection is required instead of HttpUrlConnection.
http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html
http://www.java-samples.com/java/POST-toHTTPS-url-free-java-sample-program.htm

Android: use HttpsUrlConnection for upload and get response but got SocketTimeoutException

I have a server with SSL certificate.
And I am using functions uploadFileHttps(...) to upload an image and get corresponding response with HttpsUrlConnection.
For the upload part, it sent out the image data through output stream.
However, server haven't received that image. Also, when it is trying to obtain response from server through input stream.
It catches an exception "java.net.SocketTimeoutException: Read timed out"
How could I fix it?
private static final int CONNECTION_TIMEOUT = 20000;
private static final int IMAGE_UPLOAD_TIMEOUT = 60000;
private void uploadFileHttps(File file, String requestURL) {
HttpsURLConnection connection = null;
try {
//requestURL is a https link like: https://test.abcde.com/upload
connection = (HttpsURLConnection) new URL(requestURL).openConnection();
connection.setHostnameVerifier(new NullHostNameVerifier());
connection.setConnectTimeout(CONNECTION_TIMEOUT);//timeout for handshake
connection.setReadTimeout(IMAGE_UPLOAD_TIMEOUT);//timeout for waiting read data
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "image/jpeg");
Token.setAuthTokenHeader(connection, context);
connection.setDoOutput(true);//set to use httpURLConnection for output
connection.connect();
//upload file to server
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
int bytesRead;
byte buf[] = new byte[1024];
BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
while ((bytesRead = bufInput.read(buf)) != -1) {
// write output
out.write(buf, 0, bytesRead);
out.flush();
}
out.flush();
out.close();
//get response from server
BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
System.out.println(sb.toString());
} catch (Exception e) {
if (e != null)
e.printStackTrace();
} finally {
if (connection != null) connection.disconnect();
}
}
class NullHostNameVerifier implements HostnameVerifier {
#Override
public boolean verify(String hostname, SSLSession session) {
Log.i("RestUtilImpl", "Approving certificate for " + hostname);
return true;
}
}
Stack Trace for error
W/System.err﹕ java.net.SocketTimeoutException: Read timed out
W/System.err﹕ at com.android.org.conscrypt.NativeCrypto.SSL_read(Native Method)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:689)
W/System.err﹕ at java.io.InputStream.read(InputStream.java:162)
W/System.err﹕ at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
W/System.err﹕ at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
W/System.err﹕ at com.android.okhttp.internal.Util.readAsciiLine(Util.java:316)
W/System.err﹕ at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308)
W/System.err﹕ at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135)
W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.uploadFileSSL(HttpRequestTask.java:355)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.doRequest(HttpRequestTask.java:152)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.doInBackground(HttpRequestTask.java:182)
W/System.err﹕ at com.seasonworkstation.testhttps.network.HttpRequestTask.doInBackground(HttpRequestTask.java:47)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
connection.setRequestMethod("PUT");
This sets the request method to PUT.
connection.setDoOutput(true);//set to use httpURLConnection for output
This sets the request method to POST. You need to execute this before the PUT line above.

Heavy HTTP requests from Android makes the network disconnected. How to solve this?

I got a very annoying problem now with my android development. I have a list which loading a bunch of content, each is an xml file with different address. Which means I need to sends bunch of http get request to server.
The problem if the request is less than roughly 180 times, I mean continues, very fast http request, the app just works fine. When the amount reach around 180, the connection to internet of the whole device will be lost. That means not only the app won't work, even the default browser or other apps which require internet will also not work.
This will recover after 1 minutes or two. But during this time there are no internet connection.
I do have set permissions in mifest file.
Here is my code for http connection:
public class HTTPService {
private static String response;
public HTTPService() {
response = null;
}
public static String httpGet(String requestURL) {
//Reset response's value to null
response = null;
//DEBUG LOG
Log.i("HTTPService", "Start GET request with URL " + requestURL);
//this function will do http getting action and response checking at same time.
//and assign proper information into the response string.
executeRequest(new HttpGet(requestURL));
Log.i("HTTPService", "GET request return with: " + response);
return response;
}
public static String httpPost(String requestURL, String postString) throws Exception {
//Assemble POST request
HttpPost request = new HttpPost(requestURL);
StringEntity input = new StringEntity(postString);
input.setContentType(CommonCodes.CONTENT_TYPE);
request.setEntity(input);
//DEBUG LOG
Log.i("HTTPService", "Start POST request with URL" + requestURL + ", and post content " + postString);
executeRequest(request);
return response;
}
public static void httpPut(String requestURL, String postString) throws Exception {
//Assemble PUT request
HttpPut request = new HttpPut(requestURL);
StringEntity input = new StringEntity(postString);
input.setContentType(CommonCodes.CONTENT_TYPE);
request.setEntity(input);
//DEBUG LOG
Log.i("HTTPService", "Start PUT request with URL" + requestURL + ", and put content " + postString);
executeRequest(request);
}
public static String httpDelete(String requestURL) {
HttpDelete request = new HttpDelete(requestURL);
//DEBUG LOG
Log.i("HTTPService", "Start DELETE request with URL" + requestURL);
executeRequest(request);
return response;
}
public static void executeRequest(HttpUriRequest request) {
//Check Internet Connection
if (!CheckInternetConnection.checkInternetConnection()) {
responseValidation(CommonCodes.NO_CONNECTION, 0);
}
HttpClient client = sslHttpClient();
HttpResponse httpResponse;
try {
//*********PLACE WHERE ERROR HAPPENS IN ERROR LOG!******//
httpResponse = client.execute(request);
//Check if the request success. If it success, should return 200 as status code.
if (httpResponse.getStatusLine().getStatusCode() != CommonCodes.HTTP_SUCCESS) {
//DEBUG LOG
Log.i("HTTPService", "Request failed with HTTP status code" + httpResponse.getStatusLine().getStatusCode());
//Go through checker
responseValidation(null, httpResponse.getStatusLine().getStatusCode());
}
//When request succeed, retrieve data.
//HttpEntity entity = httpResponse.getEntity();
//Convert stream data into String.
if (!httpResponse.getEntity().equals(null)) {
InputStream instream = httpResponse.getEntity().getContent();
//Convert InputStream to String.
response = convertStreamToString(instream).trim();
//Close stream
instream.close();
httpResponse.getEntity().consumeContent();
}
//Go through checker
responseValidation(response, httpResponse.getStatusLine().getStatusCode());
client.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
Log.e("HTTPService: ", "HTTP Request With ClientProtocolException Detected");
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
Log.e("HTTPService: ", "HTTP Request With IOException Detected");
client.getConnectionManager().shutdown();
e.printStackTrace();
}
}
public static void responseValidation (String response, int statusCode)
{
//Is this a HTTP Error?
if (statusCode != CommonCodes.HTTP_SUCCESS) {
response = CommonCodes.HTTP_FAIL;
}
//Is the response contains nothing?
else if (response.equals(null) || response.equals("")) {
response = CommonCodes.RESPONSE_EMPTY;
}
//Is there an internet connection?
else if (response.equals(CommonCodes.NO_CONNECTION)){
response = CommonCodes.NO_CONNECTION;
}
}
public static HttpClient sslHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocket(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 8080));
registry.register(new Scheme("https", sf, 8443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
The error message I got when the connection lost is this:
05-06 12:13:57.697 28670-28767/com.lai.android E/HTTPService:﹕ HTTP Request With IOException Detected
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://test.myworkspace.com:8080 refused
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at com.lai.android.services.http.HTTPService.executeRequest(HTTPService.java:138)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at com.lai.android.services.http.HTTPService.httpGet(HTTPService.java:73)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at com.lai.android.modules.watchlistgroup.WatchListActivity.getSingleVehicle(WatchListActivity.java:238)
05-06 12:13:57.697 28670-28767/com.lai.android W/System.err﹕ at com.lai.android.modules.watchlistgroup.WatchListActivity$InitWatchListActivity.doInBackground(WatchListActivity.java:144)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at com.lai.android.modules.watchlistgroup.WatchListActivity$InitWatchListActivity.doInBackground(WatchListActivity.java:109)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /192.168.48.95 (port 8080): connect failed: ENETUNREACH (Network is unreachable)
05-06 12:13:57.705 28670-28767/com.lai.android W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:114)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at java.net.Socket.connect(Socket.java:842)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ ... 17 more
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at libcore.io.Posix.connect(Native Method)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-06 12:13:57.713 28670-28767/com.lai.android W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
05-06 12:13:57.721 28670-28767/com.lai.android W/System.err﹕ ... 22 more
Any advice will be welcome! I'm thinking if this is caused by running up of ports. But I don't know how to solve it. Thank you!
Lucky....I solved it.
The problem is I have too many HTTP Clients instance, which used up all ports on the Phone.
Android have a max connection limit, and it takes time to release the ports which are been used. So if the requests are heavy and happens in very short time, the device just won't got enough time to release them.
The solution is instead of using new client every time, use only one client for all request.
Adding this to the code:
private static HttpClient client;
public static synchronized void createHttpClient() {
if(client == null) {
Log.i("HTTPService", "Create Client");
client = sslHttpClient();
}
}
Then call this constructor from beginning of the application.
Then replace everywhere where create new http client with the static client.
Problem solved, I will go for a beer.

HessianConnectionException: 500 Caused by: java.io.EOFException (Android)

I'm using a hessdroid proxy to communicate between my android application and my webservice/hessianservlet. It works immediatly after starting the app, but when I wait a minute and let the app call a network function again (e.g. logout()) I get this error message:
com.caucho.hessian.client.HessianConnectionException: 500: java.io.EOFException
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:197)
at $Proxy1.getPassword(Native Method)
at tsch.serviceApp.net.DataHandler.getPassword(DataHandler.java:50)
at tsch.serviceApp.PageSystemApps$1$1.run(PageSystemApps.java:91)
Caused by: java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:573)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:821)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:495)
at com.caucho.hessian.client.HessianProxy.invoke(HessianProxy.java:167)
.
public String login() {
HessianProxyFactory factory = new HessianProxyFactory();
String url = "http://192.168.56.1:8080/hessianServer";
factory.setHessian2Reply(false); // avoid hessian reply error
try {
userCon = (IUserService) factory.create(IUserService.class, url+"/IUserService");
} catch (MalformedURLException e) {
e.printStackTrace();
}
return "Logged in!";
}
public void logout() {
userCon.logout();
}
I runned into the same problem on android 4.1 ,but it's ok on android 4.0.3. I added a statement " conn.setRequestProperty("Connection", "Close");" in method openConnection of class HessianProxyFactory.
protected URLConnection openConnection(URL url)
throws IOException {
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
if (_readTimeout > 0) {
try {
conn.setReadTimeout((int) _readTimeout);
} catch (Throwable e) { // intentionally empty
}
}
conn.setRequestProperty("Content-Type", "x-application/hessian");
conn.setRequestProperty("Connection", "Close");
if (_basicAuth != null)
conn.setRequestProperty("Authorization", _basicAuth);
else if (_user != null && _password != null) {
_basicAuth = "Basic " + base64(_user + ":" + _password);
conn.setRequestProperty("Authorization", _basicAuth);
}
return conn;
}
and the problem disappeared.
Used Flamingo instead of Hessdroid and everything works fine!

Categories

Resources