0

I am trying to fetch data from an api but I am getting an following error I am unable to find out where the problem is I am using separate UI thread to fetech data then too app crashes.

2020-02-20 17:20:01.088 22802-22802/com.example.flypped E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.flypped, PID: 22802
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
    at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:415)
    at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:560)
    at okio.InputStreamSource.read(Okio.kt:102)
    at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:159)
    at okio.RealBufferedSource.request(RealBufferedSource.kt:62)
    at okio.RealBufferedSource.require(RealBufferedSource.kt:55)
    at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:299)
    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:450)
    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:429)
    at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:279)
    at okio.RealBufferedSource.select(RealBufferedSource.kt:93)
    at okhttp3.internal.Util.readBomAsCharset(Util.kt:256)
    at okhttp3.ResponseBody.string(ResponseBody.kt:187)
    at com.example.flypped.MainActivity$2$1.run(MainActivity.java:110)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7156)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
    Suppressed: android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
    at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:415)
    at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:560)
    at okio.InputStreamSource.read(Okio.kt:102)
    at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:159)
    at okio.RealBufferedSource.request(RealBufferedSource.kt:62)
    at okio.RealBufferedSource.require(RealBufferedSource.kt:55)
    at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:299)
    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:450)
    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:429)
    at okhttp3.internal.Util.skipAll(Util.kt:337)
    at okhttp3.internal.Util.discard(Util.kt:358)
    at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.close(Http1ExchangeCodec.kt:471)
    at okio.ForwardingSource.close(ForwardingSource.kt:34)
    at okhttp3.internal.connection.Exchange$ResponseBodySource.close(Exchange.kt:306)
    at okio.RealBufferedSource.close(RealBufferedSource.kt:461)
    at kotlin.io.CloseableKt.closeFinally(Closeable.kt:56)
    at okhttp3.ResponseBody.string(ResponseBody.kt:186)

Below is my code:

XML code:

 <?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">


<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_main" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottomBar"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_menu"
    android:background="#fff"
    app:itemIconTint="@drawable/nav_check"
    app:itemTextColor="@drawable/nav_check"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:id="@+id/nav_view">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            layout="@layout/nav_header"
            android:id="@+id/header"/>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/header"
            android:id="@+id/menuRecycler"
            android:layout_marginTop="20dp"/>

    </RelativeLayout>

</com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>

Java code:

 private void getMenuData(){


    OkHttpClient client = new OkHttpClient.Builder()
            .readTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20,TimeUnit.SECONDS)
            .build();

    Request request = new Request.Builder().url(URL).build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    try {

                        JSONArray jsonArray = new JSONArray(response.body().string());

                      //  Log.d("array", String.valueOf(jsonArray));

                       if(jsonArray.length() > 0){

                            for(int i=0;i<jsonArray.length();i++){

                                JSONObject jsonObject = jsonArray.getJSONObject(i);

                                String str1 = jsonObject.getString("name");
                                String str2 = jsonObject.getString("term_id");

                                MenuModel model = new MenuModel(str1,str2);
                                list.add(model);
                            }

                            adapter = new MenuAdapter(list,getApplicationContext());
                            menuRecycler.setAdapter(adapter);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void onFailure(@NotNull Call call, @NotNull final IOException e) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });
        }

    });

}

Log cat showing error in below line

JSONArray jsonArray = new JSONArray(response.body().string());

Someone please let me know what I am doing wrong.Any help would be appreciated.

THANKS

Digvijay
  • 2,887
  • 3
  • 36
  • 86

3 Answers3

0

Put your api call inside run block.

Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {

       //Your code goes here

    }
});
thread.start(); 
Dipakk Sharma
  • 976
  • 1
  • 8
  • 11
0

Callbacks run on a background thread. If you want to immediately process something in the UI you will need to post to the main thread.

private void hitApi(HttpCallBacks cb){ 
client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {

            mainHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        cb.onSuccess(null, throwable);
                    }
                });

        }

        @Override
        public void onFailure(@NotNull Call call, @NotNull final IOException e) {

             mainHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        cb.onFailure( throwable);
                    }
                });
        }

    });

}
}
interface HttpCallBacks{
    void onFailure(Throwable t);
    void onSuccess(Response r);

}
kashyap
  • 498
  • 1
  • 6
  • 21
0

Here try this code and see if it helps . Am making use of a handler on main thread, since the main issue is you can't use UI thread directly in response, neither can you update UI in background thread.

You'll need to declare 2 more global variables like:

    Handler mainHandler = new Handler(Looper.getMainLooper());
    String mStringResponse;

and write your method as:

private void getMenuData(){

        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20,TimeUnit.SECONDS)
                .build();

        Request request = new Request.Builder().url(URL).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {

                mStringResponse = String.valueOf(response.body());

                mainHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONArray jsonArray = new JSONArray(mStringResponse);
                            if (jsonArray.length() > 0) {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    String str1 = jsonObject.getString("name");
                                    String str2 = jsonObject.getString("term_id");
                                    MenuModel model = new MenuModel(str1, str2);
                                    list.add(model);
                                }
                                adapter = new MenuAdapter(list, getApplicationContext());
                                menuRecycler.setAdapter(adapter);
                            }
                        }catch (JSONException e) { }

                    }
                });
            }
            @Override
            public void onFailure(@NotNull Call call, @NotNull final IOException e) {
                mainHandler.post(new Runnable() {
                    @Override
                    public void run() { 
                          Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
                            }
             } 
        });

    }

UI changes should work like this, not sure about your adapter restrictions, but try this and check if it helps.

ljk
  • 1,496
  • 1
  • 11
  • 14