I have this CustomHttpClient class, which I found in the net:

    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<NameValuePair> 
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<NameValuePair> 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();
 }
 }
 }
    }
    }

I am using it this way:

    (...)
    entrar.setOnClickListener(new OnClickListener() {
 @Override
public void onClick(View v) {
 user=login.getText().toString();
String passtext=pass.getText().toString();
parameters=new ArrayList<NameValuePair>();
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();
    (...)

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:

    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)

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:

    public static String executeHttpPost(final String url, final 
ArrayList<NameValuePair> 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;
}  

Any help with that exception? Thank you.

Tried this last class with a phone with 2.3.4, and it works ok...

Tried with 2.3.6 version...and it does not work, neither...

Also, I tried with this library (http://loopj.com/android-async-http/) and 
still getting the same error. I have two workmates that let me try it with 
their phones, both with 2.3.6. In one of them, with the kernel 2.6.35.7, 
dont works. And in the other one, also with 2.3.6, but with kernel version 
2.6.38.6...it works.

Anybody has any idea what could i do? Thank you.

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to