SocketException(连接超时),当我在真实设备上运行我的Andr​​oid应用被抛出 [英] SocketException( Connection timed out ) is thrown when i run my android app on a real device

查看:189
本文介绍了SocketException(连接超时),当我在真实设备上运行我的Andr​​oid应用被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序需要通过HttpClient的执行HTTP请求。​​结果
我已经在模拟器上测试,它工作正常。但是当我做它用WIFI激活的真实设备上,有时会发生ANR,有时SocketException异常。结果
似乎该设备不能连接服务器。结果
logcat的打印输出如下:

此外,该应用的包名称是com.tim.wirelessorder

 九月六日至3日:51:17.803:W / System.err的(2307):java.net.SocketException异常:操作超时
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(本机方法)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:115)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:244)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
九月六日至3日:51:17.803:W / System.err的(2307):在java.net.Socket.connect(Socket.java:1055)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
九月六日至3日:51:17.803:W / System.err的(2307):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
九月六日至3日:51:17.803:W / System.err的(2307):在com.tim.wirelessorder.util.net.HttpUtil.getHtt presponse(HttpUtil.java:43)
九月六日至3日:51:17.803:W / System.err的(2307):在com.tim.wirelessorder.ui.SignIn $ BnListener.onClick(SignIn.java:117)
九月六日至3日:51:17.803:W / System.err的(2307):在android.view.View.performClick(View.java:2408)
九月六日至3日:51:17.803:W / System.err的(2307):在android.view.View $ PerformClick.run(View.java:8818)
九月六日至3日:51:17.803:W / System.err的(2307):在android.os.Handler.handleCallback(Handler.java:587)
九月六日至3日:51:17.803:W / System.err的(2307):在android.os.Handler.dispatchMessage(Handler.java:92)
九月六日至3日:51:17.803:W / System.err的(2307):在android.os.Looper.loop(Looper.java:123)
九月六日至3日:51:17.810:W / System.err的(2307):在android.app.ActivityThread.main(ActivityThread.java:4627)
九月六日至3日:51:17.810:W / System.err的(2307):在java.lang.reflect.Method.invokeNative(本机方法)
九月六日至3日:51:17.810:W / System.err的(2307):在java.lang.reflect.Method.invoke(Method.java:521)
九月六日至3日:51:17.810:W / System.err的(2307):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
九月六日至3日:51:17.810:W / System.err的(2307):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
九月六日至3日:51:17.810:W / System.err的(2307):在dalvik.system.NativeStart.main(本机方法)
九月六日至3日:51:28.289:W / KeyCharacterMap(2307):无法打开文件keycharmap
九月六日至3日:51:28.289:W / KeyCharacterMap(2307):错误加载keycharmap文件'/system/usr/keychars/ilitek_ts.kcm.bin。 hw.keyboards.65538.devname ='ilitek_ts
九月六日至3日:51:28.289:W / KeyCharacterMap(2307):使用默认的键盘对应:/system/usr/keychars/qwerty.kcm.bin

这个问题实在令人担忧,因为我不是开发一种只能在模拟器上运行的应用程序。这将没有任何意义。可以anybaby弄清楚的问题是什么?

我的code是在这里。

  HttpPost请求= HttpUtil.getHttpPost(URL);
                    清单< BasicNameValuePair>名单=新的ArrayList< BasicNameValuePair>();
                    BasicNameValuePair VNAME =新BasicNameValuePair(
                            名,名);
                    list.add(VNAME);
                    BasicNameValuePair vPwd =新BasicNameValuePair(PWD
                            PWD);
                    list.add(vPwd);
                    // Log.e(结果,KK:);
                    request.setEntity(新UrlEn codedFormEntity(列表));
                    HTT presponse响应= HttpUtil
                            .getHtt presponse(请求);
// --------上面的行抛出SocketException。这是一个静态方法。在code如下补充说。

==

 公共静态HTTPGET getHttpGet(字符串URL){
    Log.e(URL,URL);
    HTTPGET请求=新HTTPGET(URL);
    返回请求;
}
公共静态HttpPost getHttpPost(字符串URL){
    HttpPost要求=新HttpPost(URL);
    返回请求;
}
公共静态的Htt presponse getHtt presponse(HTTPGET请求)
        抛出ClientProtocolException,IOException异常{
    // Log.e(HTTP,中);
    HTT presponse响应=新DefaultHttpClient()执行(要求)。
    // Log.e(HTTP,(响应== NULL)+);
    返回响应;
}
公共静态的Htt presponse getHtt presponse(HttpPost要求)
        抛出ClientProtocolException,IOException异常{
    HTT presponse响应=新DefaultHttpClient()执行(要求)。
// -------这里它执行HttpPost要求,似乎请求失败发送。
        返回响应;
    }

这是服务器上的DO-后code。小服务程序接收请求,并获取的名称,从由用户上的UI输入的客户PWD参数。然后servlet的激活它使用的名称,PWD做查询MySQL中的DAO。你可以看到一个的System.out.println(名称)行由我希望确保该servlet是否获得该请求。当我在真实设备上测试code,控制台不给任何打印输出。我认为这意味着该servlet没有收到请求。

 公共无效的doPost(HttpServletRequest的请求,HttpServletResponse的响应)
        抛出了ServletException,IOException异常{
    OutputStream的OUT = response.getOutputStream();
    字符串名称=的request.getParameter(名称);
    字符串PWD =的request.getParameter(PWD);
    的System.out.println(名);    userDAO的DAO =新UserDAOImpl中();    字节[] R = dao.query(姓名,PWD);
    out.write(R);
    out.close();
}


解决方案

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

  URL =HTTP://<您的网址>中;
网址urlObj =新的URL(网址);
Log.d(调试,URL =+网址);
URLConnection的鲁= urlObj.openConnection();//可选 - 发送参数
字符串数据= URLEn coder.en code(参数,UTF-8)+=+ URLEn coder.en code(价值UTF-8) ; //可选 - 发送参数
//发送数据
lu.setDoOutput(真);
OutputStreamWriter WR =新OutputStreamWriter(lu.getOutputStream());
wr.write(数据);
wr.flush();
//结束可选//获取响应
RD的BufferedReader =新的BufferedReader(新的InputStreamReader(lu.getInputStream()));
串线=,解析度=;
而((行= rd.readLine())!= NULL){
    RES + =行;
}wr.flush();
wr.close();
Log.d(调试,RES =+ RES);

My android app needs to perform Http requests through HttpClient.
i've tested on the emulator and it works fine. but when i do it on a real device with WIFI activated, sometimes ANR occurred, and sometimes SocketException is thrown.
it seems that the device is not able to connect the server.
logcat print-out is as follows

in addition, the package name for the app is "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

this problem is really disturbing, because i'm not developing an app which only can be run on an emulator. that won't make any sense. can anybaby figure out what problem is?

my code is here.

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

this is the do-post code on the server. the servlet receives the request, and get the name, pwd parameter from the client entered by users on UI. then servlet activates a DAO which uses the name, pwd to do queries in MySQL. you can see a "System.out.println(name)" line by which i want to make sure whether the servlet obtains the request. and when i test the code on a real device, the console doesn't give any print-out. and i think this means the servlet doesn't receive the request.

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

解决方案

Try using another method of sending HTTP request:

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

这篇关于SocketException(连接超时),当我在真实设备上运行我的Andr​​oid应用被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆