【问题标题】:Android URLConnectionAndroid URL连接
【发布时间】:2013-04-29 06:17:14
【问题描述】:

我一直在尝试建立客户端/服务器通信。我的 servlet 在浏览器中以及从简单的 java 项目中调用时都可以正常工作。但是,当我在 Android 项目中使用相同的代码时,它不会返回任何内容。

这是我的代码:

package com.jad.carpooling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

public class HttpRequest {
    public void getTrial() {
        new Async().execute();          
    }


    public class Async extends AsyncTask <String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            Log.i(null, "entered");
            // TODO Auto-generated method stub
            URL url;
            BufferedReader reader = null;
            String s = "";
            try {
                url = new URL("http://localhost:8080/CarpoolServer/DemoServlet");
                URLConnection con = url.openConnection();
                reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String line = reader.readLine().toString();
                while ((line = reader.readLine()) != null) {
                    s = s + line;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }  catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            Log.i(null, "response: " + s);
            return s;
        }

    }

}

logcat 中没有错误,只是一堆警告。 这是logcat:

05-05 08:18:17.199: W/System.err(816):  at libcore.io.IoBridge.socket(IoBridge.java:583)
05-05 08:18:17.389: W/System.err(816):  at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
05-05 08:18:17.470: W/System.err(816):  at java.net.Socket.checkOpenAndCreate(Socket.java:663)
05-05 08:18:17.489: W/System.err(816):  at java.net.Socket.connect(Socket.java:807)
05-05 08:18:17.529: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.549: W/System.err(816):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
05-05 08:18:17.569: W/System.err(816):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-05 08:18:17.579: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.609: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.609: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.609: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.639: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:17.659: W/System.err(816):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-05 08:18:17.759: W/System.err(816):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-05 08:18:17.829: W/System.err(816):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
05-05 08:18:17.949: W/System.err(816):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-05 08:18:17.979: W/System.err(816):  at com.jad.carpooling.HttpRequest$Async.doInBackground(HttpRequest.java:73)
05-05 08:18:17.979: W/System.err(816):  at com.jad.carpooling.HttpRequest$Async.doInBackground(HttpRequest.java:1)
05-05 08:18:17.979: W/System.err(816):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-05 08:18:18.069: W/System.err(816):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-05 08:18:18.089: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:18.089: W/Trace(816): Unexpected value from nativeGetEnabledTags: 0
05-05 08:18:18.129: W/System.err(816):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-05 08:18:18.129: W/System.err(816):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-05 08:18:18.139: W/System.err(816):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-05 08:18:18.139: W/System.err(816):  at java.lang.Thread.run(Thread.java:856)
05-05 08:18:18.139: W/System.err(816): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
05-05 08:18:18.239: W/System.err(816):  at libcore.io.Posix.socket(Native Method)
05-05 08:18:18.239: W/System.err(816):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
05-05 08:18:18.239: W/System.err(816):  at libcore.io.IoBridge.socket(IoBridge.java:568)

最终的 Log.i 已记录,但字符串 's' 为空。 有什么想法吗?

【问题讨论】:

  • 这些警告是什么?可能是缺少权限?
  • 我刚刚编辑了我的问题并添加了警告

标签: android urlconnection


【解决方案1】:

从您的日志看来,它缺少 INTERNET 权限,请将其添加到您的 AndroidManifest.xml

<uses-permission  android:name="android.permission.INTERNET"></uses-permission>

关于 SO 的类似问题:

  1. Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'
  2. Why I am getting the HttpHostConnectException

更新:

Android 应用程序的本地主机是指 Mobile/Emulator 等,将其更改为您要连接的 PC 的 IP,如下所示:

url = new URL("http://MY_IP:8080/CarpoolServer/DemoServlet");

【讨论】:

  • 什么是同样的异常?你把你的许可放在哪里了?应该在 sdkversion 之后,应用之前。
  • 我认为这可能会有所帮助:05-05 08:45:12.796: W/System.err(1030): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8080) ): 连接失败:ECONNREFUSED(连接被拒绝)
  • 它已启动。我在我的安卓应用程序上尝试之前在我的浏览器上尝试过
  • 这删除了所有警告,但有两个:05-05 09:12:14.156:W/Trace(1291):来自 nativeGetEnabledTags 的意外值:0 05-05 09:12:14.216:W/IInputConnectionWrapper (1291): showStatusIcon on inactive InputConnection 但是响应仍然没有
  • @Jad 很高兴,一个原因可能是因为您忽略了您已阅读的第一行 String line = reader.readLine().toString(); 这被忽略了.相反,您可以这样做 String line = ""; while((line = reader.readLine()) != null) { }
猜你喜欢
  • 2015-09-01
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 2018-03-24
  • 2013-07-23
  • 2012-10-22
相关资源
最近更新 更多