I am looking for a way to parse a JSON-String from an URL and then pass it to GSON to deserialize it. My goal is to put the conversed text into an RecyclerViewAdapter.
I have already tried to implement the suggestions and recommendations Parsing JSON from URL but the approach does not work for me.
First I created a JavaObject-class (called Data) with 4 attributes. These attributes are identical to the fields in my URL:
public class Data {
private String title;
private String version;
private String kind;
private String etag;
public String getTitle() {
return title;
}
public String getVersion() {
return version;
}
public String getKind() {
return kind;
}
public String getEtag() {
return etag;
}
public void setTitle(String title) {
this.title = title;
}
public void setVersion(String version) {
this.version = version;
}
public void setKind(String kind) {
this.kind = kind;
}
public void setEtag(String etag) {
this.etag = etag;
}
public String toString() {
return String.format("title: %s, "
+ "version: %s, "
+ "kind: %s, "
+ "etag: %s",
title, version, kind, etag);
}
}
The process of fetching the data from my URL and the conversion via GSON is supposed to take place in the following onCreateView-method of my Fragment:
public class Fragment extends android.support.v4.app.Fragment {
public static final String FRAGMENT_TAG = "Fragment";
private OnListFragmentInteractionListener mListener;
public Fragment() {
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.i(FRAGMENT_TAG, "onCreate()");
setHasOptionsMenu(true);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(JSONS_FRAGMENT_TAG, "onCreateView()");
View view = inflater.inflate(R.layout.fragment_jsons_list, container,
false);
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
URL url = null;
try {
// Definition of the URL with the JSON-Strings
url = new URL("http://www.jsoneditoronline.org
/?id=b1b4f5e2526ab04b562723bccae30a40");
} catch (MalformedURLException e) {
e.printStackTrace();
}
InputStreamReader reader = null;
try {
// InputStreamReader is responsible to open and consume the
// URL
reader = new InputStreamReader(url.openStream());
} catch (IOException e) {
e.printStackTrace();
}
// Conversion via GSON
Data data = new Gson().fromJson(reader, Data.class);
List<Data> listdata = new ArrayList<Data>();
listdata.add(data);
recyclerView.setAdapter(new
JSONSRecyclerViewAdapter(listdata,mListener));
}
return view;
}
Every time I try to run the app it stops immediately and the logcat output is indicating that there is a problem with my onCreateView-method. The logcat output is the following:
05-08 09:21:47.179 recandroidapp E/UncaughtException:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
at java.net.InetAddress.getAllByName(InetAddress.java:752)
at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
at java.net.URL.openStream(URL.java:1057)
at recandroidapp.fragments.JSONSFragment.onCreateView(JSONSFragment.java:68)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
In order to avoid the NetworkOnMainThreadException, I have attempted to call on AsyncTask (as suggested after my initial post by users play_err_, user12345 and Aashish Aadarsh - thanks for your help!).
Unfortunately this triggers yet another error regarding the doInBackground()-method.
This is the new logcat-output I receive after implementing AsyncTask:
Do you have any ideas/suggestions? Thank you!