当前位置: 首页 > 知识库问答 >
问题:

Android URLConnection

白昊东
2023-03-14
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”为空。有什么想法吗?

共有1个答案

厉高逸
2023-03-14

从日志中显示缺少INTERNET权限将此添加到AndroidManifest.xml

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

关于SO的类似问题:

  1. 错误消息“java.net.socketexception:socket失败:EACCES(权限被拒绝)”
  2. 为什么获取HttpoStConnectException
url = new URL("http://MY_IP:8080/CarpoolServer/DemoServlet");
 类似资料:

相关问答

相关文章

相关阅读