ConnectTimeoutException:connect to / Ip Address:80 timeout

时间:2013-10-25 09:03:20

标签: android web-services rest

我遇到异常 org.apache.http.conn.ConnectTimeoutException:连接到我的计算机的/ Ipaddress:80超时当我在真实设备上运行我的Android应用程序时(设备是micromax canvas2和版本 - 4.2.1)。由于此用户无法登录,但当我在模拟器上运行此应用程序时,它已成功运行但未在真实设备上运行。我很高兴并尝试了一些解决方案,但没有实现我的目标,我仍在努力。请有人帮我解决问题。
以下是我的代码结构。

LoginActivity 中,我调用了发出http请求的方法

List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", username));
        params.add(new BasicNameValuePair("password", userpsw));
        JsonParserWebs jsonDataFromSrvr = new JsonParserWebs();
        String loginData = jsonDataFromSrvr.makeHttpReqToSrvr(loginUrl,"POST", params);


以下是用于调用webservice的 JsonParserWebs

public String makeHttpReqToSrvr(String url,String requestType,List<NameValuePair> params) {
    Log.i(JsonParserWebs.class.getName(),"URL..."+url);
    HttpEntity httpEntity=null;

    //making http request
    try {

        if (requestType == "GET") {

            //connection time out
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
            HttpConnectionParams.setSoTimeout(httpParameters, 10000);

            HttpClient httpClient = new DefaultHttpClient(httpParameters);
            String paramString =URLEncodedUtils.format(params, "utf-8");
            HttpGet httpGet = new HttpGet(url+"?"+paramString);



            HttpResponse httpResp = httpClient.execute(httpGet);
            httpEntity = httpResp.getEntity();

        }
        if (requestType == "POST") {

            //connection time out
            // From stackoverflow, I addes following three line but still got ConnectTimeoutException
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
            HttpConnectionParams.setSoTimeout(httpParameters, 10000);

            HttpClient  httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResp = httpClient.execute(httpPost);
            httpEntity = httpResp.getEntity();

        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {

        json = EntityUtils.toString(httpEntity);
        Log.v("JSON", "data"+json);
    } catch (Exception e) {
        e.printStackTrace();
    } 
    // try parse the string to a JSON object

    return json;
}

提前致谢

0 个答案:

没有答案