Search code examples
phpandroidokhttpandroid-internet

Can not get response while using okHttp3


I used okhttp to get send request to my user.php file that is on localhost server running by wamp. When I run the application I don't know why the onclick method triggered automatically (I have only one button on my XML file without setting onclick. Did it in my code). While I viewed my log cat I saw the exception: first one:

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

I added

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

but I still get same error on this case after that I get another error that says Could not execute method for Android: onClick

And this is my full error part:

  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$000(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$2.read(Okio.java:139)
07-03 07:49:05.489 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.AsyncTimeout$2.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.access$200(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$000(MainActivity.java:18)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity$1.doInBackground(MainActivity.java:39)
07-03 07:49:05.493 31858-32293/com.example.android.okhttp3 W/System.err:     at android.os.AsyncTask$2.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$1.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$000(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$2.read(Okio.java:139)
07-03 07:49:05.497 31858-32293/com.example.android.okhttp3 W/System.err:     at okio.AsyncTimeout$2.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.access$200(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$000(MainActivity.java:18)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at com.example.android.okhttp3.MainActivity$1.doInBackground(MainActivity.java:39)
07-03 07:49:05.501 31858-32293/com.example.android.okhttp3 W/System.err:     at android.os.AsyncTask$2.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$1.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)

This is my code:

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());
        }
    }
}

I get exception after


Solution

  • Please check your IP as per the emulator.

    Use 10.0.2.2 for AVD and 10.0.3.2 for genymotion.