0

I've tried to use okHttpClient command to send request with URL.

but i noticed a mistake that came to me and prevented me from showing the results. Look at the code and logcat output , and try to help me make some code adjustments. the log c

public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Object trace = getHttpResponse();
            Log.i("don_99", "" + trace);
        }



        public Object getHttpResponse() {
            OkHttpClient httpClient = new OkHttpClient();

            String url = "http://www.zoftino.com/api/storeOffers";
            Request request = new Request.Builder()
                    .url(url)
                    .build();
          com.squareup.okhttp.Response response = null;

            try {
                response = httpClient.newCall(request).execute();
                return response.body().toString();
            } catch (IOException e) {
                Log.e(TAG, "error in getting response get request okhttp");
            }
            return null;
        }
}

and here is error in logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ahmedco.api/com.ahmedco.api.MainActivity}: android.os.NetworkOnMainThreadException
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769)
                at android.app.ActivityThread.access$900(ActivityThread.java:177)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5910)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
             Caused by: android.os.NetworkOnMainThreadException
                at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
                at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                at java.net.InetAddress.getAllByName(InetAddress.java:215)
                at com.squareup.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
                at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
                at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
                at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
                at com.squareup.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:344)
                at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:327)
                at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
                at com.squareup.okhttp.Call.getResponse(Call.java:267)
                at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
                at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
                at com.squareup.okhttp.Call.execute(Call.java:79)
                at com.ahmedco.api.MainActivity.getHttpResponse(MainActivity.java:82)
                at com.ahmedco.api.MainActivity.test5(MainActivity.java:66)
                at com.ahmedco.api.MainActivity.onCreate(MainActivity.java:61)
Edric
  • 24,639
  • 13
  • 81
  • 91
ibrahem ahmed
  • 25
  • 1
  • 4
  • The exception tells you what the problem is: `android.os.NetworkOnMainThreadException`. You cannot make requests on the main thread. Create a new thread or task with all the networking logic and use your HTTP client there. – QBrute Jan 18 '20 at 09:29
  • Try this :- new Thread( new Runnable() { @Override public void run() { Object trace = getHttpResponse(); Log.i("don_99", "" + trace); } } ).start(); – Sabinmon ks Jan 18 '20 at 09:36
  • Oky Thanks, now resolve it – ibrahem ahmed Jan 18 '20 at 09:49

0 Answers0