单击按钮以点击api时出现以下错误

时间:2019-06-11 12:07:52

标签: android

我已经在android中进行了注册活动。 在单击按钮时,我用URL命中了一个api: task.execute(“ http://10.0.2.2:8080/TestWebService_war_exploded/hello”);

我也使用了127.0.0.1而不是10.0.0.2 但这也不起作用。

单击按钮后,我在日志中遇到以下错误

  

W / System.err:java.net.UnknownServiceException:CLEARTEXT   网络安全策略不允许与10.0.2.2进行通信           在okhttp3.internal.connection.RealConnection.connect(RealConnection.java:146)           在okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)           在okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)           在okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)           在okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)   W / System.err:位于   okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)           在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)           在okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)           在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)           在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)           在okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)           在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)           在okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)           在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)           在okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)   W / System.err:位于   okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)           在okhttp3.RealCall.execute(RealCall.java:77)           在com.example.android.complaintbox_ritesltd.activity_SignUp $ DownloadWebPageTask.doInBackground(activity_SignUp.java:70)           在com.example.android.complaintbox_ritesltd.activity_SignUp $ DownloadWebPageTask.doInBackground(activity_SignUp.java:60)           在android.os.AsyncTask $ 2.call(AsyncTask.java:333)           在java.util.concurrent.FutureTask.run(FutureTask.java:266)           在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245)           在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)           在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)   W / System.err:位于java.lang.Thread.run(Thread.java:764)

代码为:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        // we use the OkHttp library from https://github.com/square/okhttp
        OkHttpClient client = new OkHttpClient();
        try{
            Request request =
                    new Request.Builder()
                            .url(urls[0])
                            .build();
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        }catch(Exception e){
            e.printStackTrace();
        }


        return "Download failed";
    }

    @Override
    protected void onPostExecute(String result) {
        textView.setText(result);
    }
}

// Triggered via a button in your layout
public void onClick(View view) {


    DownloadWebPageTask task = new DownloadWebPageTask();
    task.execute("http://10.0.2.2:8080/TestWebService_war_exploded/hello");

}

}

清单中的所有更改已经完成。

我正在尝试在模拟器上运行它。

1 个答案:

答案 0 :(得分:1)

在您的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
 <manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
    ...
    android:usesCleartextTraffic="true"
    ...
</application>
</manifest>

还要在 AndroidManifest.xml

中添加Internet权限
<uses-permission android:name="android.permission.INTERNET" />