Issue
I'm getting a crash in an Android device which shows an exception about android.os.NetworkOnMainThreadException
in AsyncTask
. Why do I get this exception? The exception is on line 60 in onPreExecute
.
I send context with WeakReference. Please explain the advantage of using WeakReference here.
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rview);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
MyTask task = new MyTask(this);
task.execute();
}
static class MyTask extends AsyncTask {
BufferedReader reader;
private WeakReference < MainActivity > contextRef;
MyTask(MainActivity mainActivity) {
contextRef = new WeakReference < >(mainActivity);
}
@Override
protected void onPreExecute() {
try {
URL url = new URL("https://api.myjson.com/bins/1fi1zm");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
} catch(MalformedURLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
@Override
protected Object doInBackground(Object[] objects) {
return null;
}
@Override
protected void onPostExecute(Object o) {
MainActivity context = contextRef.get();
if (context != null) {
Gson gson = new Gson();
List < Contact > list;
try {
list = Arrays.asList(gson.fromJson(reader.readLine(), Contact[].class));
context.adapter = new RecyclerViewAdapter(list);
context.recyclerView.setAdapter(context.adapter);
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
Solution
The issue is for the code in onPreExecute(). It runs on Main Thread and you cannot do background task here. Try this
static class MyTask extends AsyncTask {
BufferedReader reader;
private WeakReference<MainActivity> contextRef;
MyTask(MainActivity mainActivity) {
contextRef = new WeakReference<>(mainActivity);
}
@Override
protected void onPreExecute() {
}
@Override
protected Object doInBackground(Object[] objects) {
try {
URL url = new URL("https://api.myjson.com/bins/1fi1zm");
URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
MainActivity context = contextRef.get();
if (context != null) {
Gson gson = new Gson();
List<Contact> list;
try {
list = Arrays.asList(gson.fromJson(reader.readLine(), Contact[].class));
context.adapter = new RecyclerViewAdapter(list);
context.recyclerView.setAdapter(context.adapter);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Let me know if it solved your issue.
Answered By - Hari N Jha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.