使用 okHttp3 时无法得到响应

Can not get response while using okHttp3

我使用 okhttp 通过 wamp 向我的 user.php 文件发送请求,该文件位于本地主机服务器 运行ning 上。当我 运行 应用程序时,我不知道为什么 onclick 方法会自动触发(我的 XML 文件上只有一个按钮而没有设置 onclick。在我的代码中做到了)。当我查看我的日志猫时,我看到了异常: 第一个:

Http3: ran into exception: socket failed: EACCES (Permission denied)

我加了

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

但在这种情况下我仍然遇到同样的错误 之后我收到另一个错误,提示 Could not execute method for Android: onClick

这是我的全部错误部分:

  07-03 07:48:55.453 31858-32293/com.example.android.okhttp3 D/Http3: get Function Called
07-03 07:48:55.453 31858-32293/com.example.android.okhttp3 D/Http3: Client Created
07-03 07:48:55.453 31858-32293/com.example.android.okhttp3 D/Http3: Request Build successful
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err: java.net.SocketTimeoutException
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at java.net.PlainSocketImpl.access[=12=]0(PlainSocketImpl.java:46)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.Okio.read(Okio.java:139)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.AsyncTimeout.read(AsyncTimeout.java:211)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine.access0(HttpEngine.java:81)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall.getResponse(RealCall.java:241)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall.execute(RealCall.java:57)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity.DoGetRequest(MainActivity.java:59)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity.access[=12=]0(MainActivity.java:18)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity.doInBackground(MainActivity.java:39)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:287)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at java.lang.Thread.run(Thread.java:856)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 D/Http3: ran into exception: null
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err: java.net.SocketTimeoutException
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at java.net.PlainSocketImpl.read(PlainSocketImpl.java:491)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at java.net.PlainSocketImpl.access[=12=]0(PlainSocketImpl.java:46)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.Okio.read(Okio.java:139)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.AsyncTimeout.read(AsyncTimeout.java:211)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine.access0(HttpEngine.java:81)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall.getResponse(RealCall.java:241)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at okhttp3.RealCall.execute(RealCall.java:57)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity.DoGetRequest(MainActivity.java:59)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity.access[=12=]0(MainActivity.java:18)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity.doInBackground(MainActivity.java:39)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:287)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-03 07:49:05.505 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-03 07:49:05.505 31858-32293/com.example.android.okhttp3 W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
07-03 07:49:05.505 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-03 07:49:05.505 31858-32293/com.example.android.okhttp3 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-03 07:49:05.505 31858-32293/com.example.android.okhttp3 W/System.err:     at java.lang.Thread.run(Thread.java:856)

这是我的代码:

package com.example.android.okhttp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button GetReuqest,PostRequest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GetReuqest=(Button)findViewById(R.id.getrequest);
        GetReuqest.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.getrequest: {
                 new AsyncTask() {
                @Override
                protected Object doInBackground(Object[] params) {
                    DoGetRequest();
                    return null;
                }
            }.execute();
            }

        }

    }
    private void DoGetRequest()
    {
        Log.d("Http3","get Function Called");
        String url="http://10.0.2.2/user.php";
        OkHttpClient client=new OkHttpClient();
        Log.d("Http3","Client Created");
        Request newRequest=new Request.Builder()
                .url(url)
                .build();
        Log.d("Http3","Request Build successful");
        try {
            Response response=client.newCall(newRequest).execute();
            Log.d("Http3","Get Called responese");
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("Http3","ran into exception: "+e.getMessage());
        }
    }
}

我在

之后得到异常

在您的 AndroidManifest.xml 文件中授予以下权限

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

Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) at libcore.io.IoBridge.connectErrno(IoBridge.java:144) at libcore.io.IoBridge.connect(IoBridge.java:112)

在这里,您基本上是在尝试在主线程上进行网络调用。试试下面的代码

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.getrequest: {
             new AsyncTask() {
         @Override
         protected Object doInBackground(Object[] params) {
           DoGetRequest();
       }
  }.execute();

        }

    }

}

建议:GetReuqest,PostRequest,DoGetRequest()请遵循驼峰式

请根据模拟器检查您的IP。

对 AVD 使用 10.0.2.2,对 genymotion 使用 10.0.3.2