Note that there are some explanatory texts on larger screens.

plurals
  1. POCustomHttpClient throwing timeout exception
    primarykey
    data
    text
    <p>I have this CustomHttpClient class, which I found in the net:</p> <pre><code>public class CustomHttpClient { public static final int HTTP_TIMEOUT=10000; private static HttpClient mHttpClient; private static HttpClient getHttpClient(){ if(mHttpClient==null){ mHttpClient = new DefaultHttpClient(); final HttpParams params = mHttpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT); HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT); ConnManagerParams.setTimeout(params, HTTP_TIMEOUT); } return mHttpClient; } public static String executeHttpPost(String url, ArrayList&lt;NameValuePair&gt; postParameters) throws Exception { BufferedReader in = null; try{ HttpClient client = getHttpClient(); HttpPost request = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request);//This is line 45, where the exception is thrown. 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 result = sb.toString(); return result; }finally{ if (in!=null){ try{ in.close(); }catch(IOException e){ e.printStackTrace(); } } } } public static HttpResponse ResponseExecuteHttpPost(String url, ArrayList&lt;NameValuePair&gt; postParameters) throws Exception { BufferedReader in = null; try{ HttpClient client = getHttpClient(); HttpPost request = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request); return response; }finally{ if (in!=null){ try{ in.close(); }catch(IOException e){ e.printStackTrace(); } } } } public static String executeHttpGet(String url) throws Exception { BufferedReader in = null; try{ HttpClient client = getHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(url)); HttpResponse response = client.execute(request); 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 result = sb.toString(); return result; } finally{ if(in!=null){ try{ in.close(); }catch (IOException e){ e.printStackTrace(); } } } } } </code></pre> <p>I am using it this way:</p> <pre><code>(...) entrar.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { user=login.getText().toString(); String passtext=pass.getText().toString(); parameters=new ArrayList&lt;NameValuePair&gt;(); parameters.add(new BasicNameValuePair("j_username", user)); parameters.add(new BasicNameValuePair("j_password", passtext)); parameters.add(new BasicNameValuePair("movil", "si")); respuesta=null; try{ Thread thread=new Thread(){ public void run() { try { respuesta=CustomHttpClient.executeHttpPost(urlogin, parameters); res=respuesta.toString(); (...) </code></pre> <p>I am aware that in 4.2.2 version of Android, every http call must be in another thread or it will rise an exception, but as you can see, the call is in another thread...but I am still getting a TimeoutException:</p> <pre><code>05-08 10:25:12.801: W/System.err(3191): org.apache.http.conn.ConnectTimeoutException: Connection timed out 05-08 10:25:12.811: W/System.err(3191): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123) 05-08 10:25:12.821: W/System.err(3191): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 05-08 10:25:12.826: W/System.err(3191): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 05-08 10:25:12.831: W/System.err(3191): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 05-08 10:25:12.836: W/System.err(3191): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 05-08 10:25:12.841: W/System.err(3191): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 05-08 10:25:12.851: W/System.err(3191): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 05-08 10:25:12.871: W/System.err(3191): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 05-08 10:25:12.886: W/System.err(3191): at com.publidirecta.vinceriazafata.CustomHttpClient.executeHttpPost(CustomHttpClient.java:45) 05-08 10:25:12.891: W/System.err(3191): at com.publidirecta.vinceriazafata.LoginActivity$3$1.run(LoginActivity.java:121) </code></pre> <p>The server is up, and the url is ok. I have tried surrounding the part of the code inside the CustomHttpClient class inside another thread, this way, but still doesn't works:</p> <pre><code>public static String executeHttpPost(final String url, final ArrayList&lt;NameValuePair&gt; postParameters) throws Exception { Thread thread=new Thread(){ public void run(){ BufferedReader in = null; try{ HttpClient client = getHttpClient(); HttpPost request = new HttpPost(url); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request); 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(); result = sb.toString(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if (in!=null){ try{ in.close(); }catch(IOException e){ e.printStackTrace(); } } } } }; thread.start(); return result; } </code></pre> <p>Any help with that exception? Thank you.</p> <p>EDIT: Tried this las class with a phone with 2.3.4, and it works ok...</p> <p>Tried with 2.3.6 version...and it does not work, neither...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload