【问题标题】:HttpRequest times outHttpRequest 超时
【发布时间】:2014-11-29 10:51:23
【问题描述】:

我正在尝试使用以下格式从我的应用向我的计算机发出获取请求 my_ip:8888/msg/my_message 当我打开连接时,它可以工作,但执行永远不会超过执行(请求)部分。它在执行获取请求之前超时(1 分钟到)。请求 url 是正确的,因为我直接在我的电脑浏览器上尝试过。 我的 android 清单具有互联网权限。 这是代码

    private static JSONObject sendPetition(HttpUriRequest request) throws Exception{
        AndroidHttpClient client;

        HttpResponse response;
        JSONObject responseJSON;

        client = AndroidHttpClient.newInstance("IPM-HttpClient/UNAVAILABLE");
        response=  client.execute(request);
        responseJSON = new JSONObject(EntityUtils.toString(response.getEntity()));
        client.close();
        return responseJSON;
    }

编辑 1:

好像我没有提供足够的信息。抱歉:这是日志

11-29 12:10:21.503 5546-5546/com.example.alvarofeal.aplicacioncliente I/View: 触摸调度到 android.widget.Button{41f3ede0 VFED..C. ...P.... 311,754-407,826 #7f080002 app:id/button}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=59.31427, y[0]=32.06659, toolType [0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=2125112, downTime=2124997, deviceId=4, source=0x1002 }

11-29 12:10:21.504 5546-5546/com.example.alvarofeal.aplicacioncliente V/Provider/Settings:来自设置缓存,名称 = sound_effects_enabled ,值 = 0

11-29 12:10:21.506 5546-5546/com.example.alvarofeal.aplicacioncliente D/dalvikvm: 创建 interp 线程:堆栈大小=128KB

11-29 12:10:21.506 5546-5546/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕创建新线程

11-29 12:10:21.507 5546-5546/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕已创建新线程

11-29 12:10:21.507 5546-5546/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕更新线程列表

11-29 12:10:21.507 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=12: interp stack at 0x6126a000

11-29 12:10:21.507 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=12: created from interp

11-29 12:10:21.507 5546-5546/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕启动新线程

11-29 12:10:21.507 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=12: 通知调试器

11-29 12:10:21.507 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=12 (AsyncTask #1): 调用 run()

11-29 12:10:21.509 5546-5669/com.example.alvarofeal.aplicacioncliente I/System.out: httpget:http://192.168.0.8:8888/msg/play/eABHFSQKCdU

11-29 12:10:21.512 5546-5669/com.example.alvarofeal.aplicacioncliente I/System.out:http://192.168.0.8:8888/msg/play/eABHFSQKCdU

11-29 12:10:21.519 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕创建 interp 线程:堆栈大小=128KB

11-29 12:10:21.519 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕创建新线程

11-29 12:10:21.519 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕已创建新线程

11-29 12:10:21.519 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕更新线程列表

11-29 12:10:21.519 5546-5670/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=13: interp stack at 0x6138a000

11-29 12:10:21.519 5546-5670/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=13: created from interp

11-29 12:10:21.519 5546-5669/com.example.alvarofeal.aplicacioncliente D/dalvikvm﹕启动新线程

11-29 12:10:21.520 5546-5670/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=13: 通知调试器

11-29 12:10:21.520 5546-5670/com.example.alvarofeal.aplicacioncliente D/dalvikvm: threadid=13 (RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@41fae950): 调用运行()

11-29 12:10:21.533 5546-5669/com.example.alvarofeal.aplicacioncliente I/System.out:[socket][0] connection /192.168.0.8:8888;LocalPort=34838(60000)

11-29 12:10:21.533 5546-5669/com.example.alvarofeal.aplicacioncliente I/System.out﹕[CDS]connect[/192.168.0.8:8888] tm:60

11-29 12:10:21.534 5546-5669/com.example.alvarofeal.aplicacioncliente D/Posix: [Posix_connect Debug]Process com.example.alvarofeal.aplicacioncliente :8888

之后它只是抛出一个连接超时异常:

编辑3:

11-29 12:27:19.988 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err: org.apache.http.conn.ConnectTimeoutException: Connect to /192.168.0.8:8888 timed out

11-29 12:27:19.990 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)

11-29 12:27:19.991 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)

11-29 12:27:19.992 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)

11-29 12:27:19.992 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)

11-29 12:27:19.993 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)

11-29 12:27:19.994 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:587)

11-29 12:27:19.995 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:511)

11-29 12:27:19.996 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:489)

11-29 12:27:19.996 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err: at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:260)

11-29 12:27:19.997 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err﹕在 com.example.alvarofeal.model.Model.sendPetition(Model.java:29)

11-29 12:27:19.998 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 com.example.alvarofeal.model.Model.playVideo(Model.java:38)

11-29 12:27:19.999 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 com.example.alvarofeal.aplicacioncliente.VideoList$Videos.doInBackground(VideoList.java:170)

11-29 12:27:20.000 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 com.example.alvarofeal.aplicacioncliente.VideoList$Videos.doInBackground(VideoList.java:165)

11-29 12:27:20.003 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 android.os.AsyncTask$2.call(AsyncTask.java:288)

11-29 12:27:20.005 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 java.util.concurrent.FutureTask.run(FutureTask.java:237)

11-29 12:27:20.007 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)

11-29 12:27:20.009 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

11-29 12:27:20.012 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

11-29 12:27:20.013 6207-6277/com.example.alvarofeal.aplicacioncliente W/System.err:在 java.lang.Thread.run(Thread.java:841)

设置: 我正在使用我的 android 设备,我正在尝试与我的计算机(端口 8888)通信。他们都在同一个网络中。

【问题讨论】:

  • 你可以给你发AndroidHttpClient类吗?
  • @rom4ek 是导入的 android.net.http.AndroidHttpClient;
  • using the format。请准确说出您使用的网址。您的代码对于调试此问题不是很有用,因为所涉及的操作是隐藏的。如果超时,您将在 logcat 中提到异常或错误。请发帖。
  • When I open the connection it works。你是什​​么意思?什么有效?
  • @greenapps 添加了日志,以便您可以看到应用程序中的 url 和 waht。

标签: android http get server


【解决方案1】:

我认为您应该将超时设置为您的请求,如下所示:

   private static JSONObject sendPetition(HttpUriRequest request) throws Exception{
        AndroidHttpClient client;
        int timeout = 5000

        HttpResponse response;
        JSONObject responseJSON;

        client = AndroidHttpClient.newInstance("IPM-HttpClient/UNAVAILABLE");

        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
        HttpConnectionParams.setSoTimeout(httpParams, timeout);

        request.setParams(httpParams);

        response = client.execute(request);
        responseJSON = new JSONObject(EntityUtils.toString(response.getEntity()));
        client.close();
        return responseJSON;
    }

【讨论】:

  • 我已经试过了...我认为这不是代码问题,因为我什至无法使用设备浏览器发送请求
  • 所以我认为存在服务器端问题,而不是Android。您应该使用您的服务器代码创建另一个答案。
  • 是的,起初我不知道这就是我发布此内容的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
相关资源
最近更新 更多