0

我有这个 CustomHttpClient 类,我在网上找到的:

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

我这样使用它:

(...)
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();
(...)

我知道在 4.2.2 版本的 Android 中,每个 http 调用都必须在另一个线程中,否则会引发异常,但正如您所见,调用在另一个线程中......但我仍然收到 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)

服务器已启动,并且 url 正常。我已经尝试在另一个线程中的 CustomHttpClient 类中包含代码的一部分,但仍然无法正常工作:

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;
}  

这个例外有什么帮助吗?谢谢你。

编辑:用 2.3.4 的手机尝试了这个 las 课程,它工作正常......

尝试使用 2.3.6 版本......它不起作用,也......

4

0 回答 0