0

I have made a sign up activity in android. On clicking the button, I am hitting an api with url: task.execute("http://10.0.2.2:8080/TestWebService_war_exploded/hello");

I have used 127.0.0.1 instead of 10.0.0.2 also but that also doesn't work.

I am getting the following errors in log once I click the button

W/System.err: java.net.UnknownServiceException: CLEARTEXT communication to 10.0.2.2 not permitted by network security policy at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:146) at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257) at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135) at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) at okhttp3.RealCall.execute(RealCall.java:77) at com.example.android.complaintbox_ritesltd.activity_SignUp$DownloadWebPageTask.doInBackground(activity_SignUp.java:70) at com.example.android.complaintbox_ritesltd.activity_SignUp$DownloadWebPageTask.doInBackground(activity_SignUp.java:60) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/System.err: at java.lang.Thread.run(Thread.java:764)

code is given as:

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");

}

}

Already did all the changes in manifest ..

I am trying to run it on emulator..

1 Answers1

0

In your AndroidManifest.xml

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

also add internet permission in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
Praveen
  • 946
  • 6
  • 14