当我在真实设备上运行我的Android应用程序时抛出SocketException(连接超时)

时间:2012-06-03 02:22:43

标签: android socketexception connection-timeout

我的Android应用程序需要通过HttpClient执行Http请求 我已经在模拟器上测试过,它工作正常。但是当我在激活WIFI的真实设备上执行此操作时,有时会发生ANR,有时会抛出SocketException。
似乎设备无法连接服务器 logcat打印输出如下

此外,该应用程序的软件包名称为“com.tim.wirelessorder”

06-03 09:51:17.803: W/System.err(2307): java.net.SocketException: The operation timed out
06-03 09:51:17.803: W/System.err(2307):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
06-03 09:51:17.803: W/System.err(2307):     at java.net.Socket.connect(Socket.java:1055)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-03 09:51:17.803: W/System.err(2307):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-03 09:51:17.803: W/System.err(2307):     at com.tim.wirelessorder.util.net.HttpUtil.getHttpResponse(HttpUtil.java:43)
06-03 09:51:17.803: W/System.err(2307):     at com.tim.wirelessorder.ui.SignIn$BnListener.onClick(SignIn.java:117)
06-03 09:51:17.803: W/System.err(2307):     at android.view.View.performClick(View.java:2408)
06-03 09:51:17.803: W/System.err(2307):     at android.view.View$PerformClick.run(View.java:8818)
06-03 09:51:17.803: W/System.err(2307):     at android.os.Handler.handleCallback(Handler.java:587)
06-03 09:51:17.803: W/System.err(2307):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 09:51:17.803: W/System.err(2307):     at android.os.Looper.loop(Looper.java:123)
06-03 09:51:17.810: W/System.err(2307):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-03 09:51:17.810: W/System.err(2307):     at java.lang.reflect.Method.invokeNative(Native Method)
06-03 09:51:17.810: W/System.err(2307):     at java.lang.reflect.Method.invoke(Method.java:521)
06-03 09:51:17.810: W/System.err(2307):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-03 09:51:17.810: W/System.err(2307):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-03 09:51:17.810: W/System.err(2307):     at dalvik.system.NativeStart.main(Native Method)
06-03 09:51:28.289: W/KeyCharacterMap(2307): Can't open keycharmap file
06-03 09:51:28.289: W/KeyCharacterMap(2307): Error loading keycharmap file '/system/usr/keychars/ilitek_ts.kcm.bin'. hw.keyboards.65538.devname='ilitek_ts'
06-03 09:51:28.289: W/KeyCharacterMap(2307): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

这个问题真的令人不安,因为我没有开发一个只能在模拟器上运行的应用程序。这没有任何意义。 anybaby可以找出问题所在吗?

我的代码在这里。

                    HttpPost request = HttpUtil.getHttpPost(url);
                    List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
                    BasicNameValuePair vName = new BasicNameValuePair(
                            "name", name);
                    list.add(vName);
                    BasicNameValuePair vPwd = new BasicNameValuePair("pwd",
                            pwd);
                    list.add(vPwd);
                    // Log.e("result", "kk:");
                    request.setEntity(new UrlEncodedFormEntity(list));
                    HttpResponse response = HttpUtil
                            .getHttpResponse(request);
// -------- the line above throws the SocketException. this is a static method. the code is added below.

======

public static HttpGet getHttpGet(String url) {
    Log.e("url", url);
    HttpGet request = new HttpGet(url);
    return request;
}


public static HttpPost getHttpPost(String url) {
    HttpPost request = new HttpPost(url);
    return request;
}
public static HttpResponse getHttpResponse(HttpGet request)
        throws ClientProtocolException, IOException {
    // Log.e("http", "in ");
    HttpResponse response = new DefaultHttpClient().execute(request);
    // Log.e("http", (response == null) + " ");
    return response;
}


public static HttpResponse getHttpResponse(HttpPost request)
        throws ClientProtocolException, IOException {
    HttpResponse response = new DefaultHttpClient().execute(request);
// ------- and here it performs a HttpPost request, and it seems the request fails to be sent.
        return response;
    }

这是服务器上的do-post代码。 servlet接收请求,并从用户在UI上输入的客户端获取名称pwd参数。然后servlet激活一个DAO,它使用名称pwd在MySQL中进行查询。你可以看到一个“System.out.println(name)”行,我想通过它来确定servlet是否获得了请求。当我在真实设备上测试代码时,控制台不会打印出任何内容。我认为这意味着servlet没有收到请求。

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    OutputStream out = response.getOutputStream();
    String name = request.getParameter("name");
    String pwd = request.getParameter("pwd");
    System.out.println(name);

    UserDao dao = new UserDaoImpl();

    byte[] r = dao.query(name, pwd);
    out.write(r);
    out.close();
}

2 个答案:

答案 0 :(得分:1)

当我没有数据连接时,我什么时候得到它。同时检查您是否拥有可以执行此操作的权限。

答案 1 :(得分:0)

尝试使用另一种发送HTTP请求的方法:

url = "http://<your URL>";
URL urlObj = new URL(url);
Log.d("DEBUG", "url=" + url);
URLConnection lu = urlObj.openConnection();

//optional - send parameters
String data = URLEncoder.encode("param", "UTF-8") + "=" + URLEncoder.encode(value, "UTF-8"); //optional - send parameters
// Send data
lu.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(lu.getOutputStream());
wr.write(data);
wr.flush();
//end of "optional"

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(lu.getInputStream()));
String line = "", res = "";
while ((line = rd.readLine()) != null) {
    res += line;
}

wr.flush();
wr.close();
Log.d("DEBUG", "res=" + res);