0

我一直在尝试建立客户端/服务器通信。我的 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中没有错误,只是一堆警告。这是日志猫:

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 已记录,但 String 's' 为空。有任何想法吗?

4

1 回答 1

1

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

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

关于 SO 的类似问题:

  1. 错误消息“java.net.SocketException:套接字失败:EACCES(权限被拒绝)”
  2. 为什么我收到 HttpHostConnectException

更新:

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

url = new URL("http://MY_IP:8080/CarpoolServer/DemoServlet");
于 2013-05-05T08:29:32.310 回答