web server fails to respond sometimes
dmoody256
Posts: 2
i have been trying to get the spinneret to talk to an android application i'm creating. I used the tutorial mike G has at http://spinneret.servebeer.com:5000 to setup the web server. The server is working great in browser from PC's, however it seems to have problems with the browser on my android phone. Also in my android application i used Apache HTTP components to send a get to the web server. It sometimes fails to respond and sometimes works just like the browser from my android phone.
I also tried this with a regular java application and it still would fail to respond sometimes, but the java Apache components in the java version automatically caught the NoHttpResponseException and resent request till the server responded. The Java version seems to only need to resend the request once and it will get a resonse, but the android version needs to resend multiple times before a successful response.
Also i have been using Eclipse as an IDE for both versions (android and regular java), and the code is the same for both.
For now i have been just re-sending the request till the server responds successfully for my android application, but I am hoping for a better solution, so i was wondering if anybody else knows about this or has any ideas.
Below is my java code and the console that the java code. my server will be up and running so feel free to try it out.
I also tried this with a regular java application and it still would fail to respond sometimes, but the java Apache components in the java version automatically caught the NoHttpResponseException and resent request till the server responded. The Java version seems to only need to resend the request once and it will get a resonse, but the android version needs to resend multiple times before a successful response.
Also i have been using Eclipse as an IDE for both versions (android and regular java), and the code is the same for both.
For now i have been just re-sending the request till the server responds successfully for my android application, but I am hoping for a better solution, so i was wondering if anybody else knows about this or has any ideas.
Below is my java code and the console that the java code. my server will be up and running so feel free to try it out.
Sep 4, 2012 5:30:35 PM org.apache.http.impl.client.DefaultRequestDirector tryExecuteINFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond Sep 4, 2012 5:30:36 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute INFO: Retrying request on
import java.io.BufferedReader;import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.HttpProtocolParams; public class Test { public static void main(String args[]){ Test test = new Test(); try { test.executeHttpGet(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void executeHttpGet() throws Exception { BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); HttpProtocolParams.setUseExpectContinue(client.getParams(), false); request.setURI(new URI("http://142.197.135.16:5000/aled.htm?led=on")); HttpResponse response = client.execute(request); //code i used to retry the request till server resonds /*HttpResponse response = null; boolean RetryConnection = true; int retrycount = 0; while(RetryConnection == true && retrycount < 10) { try{ response = client.execute(request); RetryConnection = false; }catch(IOException e) { System.out.println("cuaght"); retrycount += 1; Thread.sleep(retrycount*100); } }*/ in = new BufferedReader (new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String page = sb.toString(); System.out.println(page); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Comments
Some folks have had luck using this setting around line 270 in the HTTPServer.
Others have increased the values for DELAY and MAX_TRIES.
Thanks mike.