2013-09-01 65 views
0

嗨,我使用LoopJ库来获取服务器在json中的响应。但问题是,有时我得到org.apache.http.conn.ConnectTimeoutException,有时它运行良好。我正在使用GET方法。但是,当我复制并粘贴URL到我的浏览器它运行良好但在Android设备上,我大多不能连接到服务器是什么问题。我究竟做错了什么 ?Android异步Http客户端

client.setTimeout(timoutVal); 
client.get(
    "http://somewebsiteaddress.com/users.php?task=isUserPresent&email=" 
     + URLEncoder.encode(username) + "&password=" 
     + URLEncoder.encode(password) + "&licenseKey=" 
     + URLEncoder.encode(licKey), null, 
    new AsyncHttpResponseHandler() { 

     public void onSuccess(String response) { 

     loading.cancel(); 

     Log.v("web response ", response); 

     try { 
      json = new JSONObject(response); 

      if (json.getBoolean("status")) { 

      delegate.Validation(
       json.getBoolean("isUserPresent"), 

       json.getBoolean("license"), username, 
       password, json); 

      } else { 

      delegate.invalidLogin(); 
      } 

     } catch (JSONException e) { 

      e.printStackTrace(); 
     } 

     } 

     @Override 
     public void onFailure(Throwable arg) { 

     Log.v("onFailure", arg + ""); 

     loading.cancel(); 

     delegate.InternetErrorDialog(); 

     super.onFailure(arg); 
     } 

    }); 
+0

只要提一下原因:http://developer.android.com/reference/org/apache/http/conn/ConnectTimeoutException.html可能是因为连接到HTTP服务器或等待可用连接时出现“超时” 。 –

回答

0

尝试增加超时值。 原因是客户端会尝试获取响应,直到超时值。 只要超过超时值,就会抛出超时异常。