java - IllegalStateException when trying to parse JSON Feed with retrofit -
i'm trying display feed cardviews in recyclerview using json , retrofit library. have been working on problem 4 hours , 1 hour searching google now, still, no success.
no matter try following error:
d/error: java.lang.illegalstateexception: expected begin_object begin_array @ line 1 column 2 path $ d/error: java.lang.illegalstateexception: expected begin_object begin_array @ line 1 column 2 path $
json example php api:
[{"id":"143","uploadid":"36","type":"excellent","userid":"58","username":"doitlikeaves","date":"31 may","timestamp":"2016-05-31 22:37:50","title":"ph zusammenfassung #2","subject":"physik"},{"id":"142","uploadid":"36","type":"presentation","userid":"58","username":"doitlikeaves","date":"31 may","timestamp":"2016-05-31 21:57:21","title":"ph zusammenfassung #2","subject":"physik"},{"id":"141","uploadid":"61","type":"document","userid":"56","username":"maja","date":"31 may","timestamp":"2016-05-31 14:29:00","title":" rev.","subject":"geschichte"}]
android files:
feedelement.js:
import com.google.gson.annotations.expose; import com.google.gson.annotations.serializedname; public class feedelement { @serializedname("id") @expose private string id; @serializedname("uploadid") @expose private string uploadid; @serializedname("type") @expose private string type; @serializedname("userid") @expose private string userid; @serializedname("username") @expose private string username; @serializedname("date") @expose private string date; @serializedname("timestamp") @expose private string timestamp; @serializedname("title") @expose private string title; @serializedname("subject") @expose private string subject; /** * * @return * id */ public string getid() { return id; } /** * * @param id * id */ public void setid(string id) { this.id = id; } /** * * @return * uploadid */ public string getuploadid() { return uploadid; } /** * * @param uploadid * uploadid */ public void setuploadid(string uploadid) { this.uploadid = uploadid; } /** * * @return * type */ public string gettype() { return type; } /** * * @param type * type */ public void settype(string type) { this.type = type; } /** * * @return * userid */ public string getuserid() { return userid; } /** * * @param userid * userid */ public void setuserid(string userid) { this.userid = userid; } /** * * @return * username */ public string getusername() { return username; } /** * * @param username * username */ public void setusername(string username) { this.username = username; } /** * * @return * date */ public string getdate() { return date; } /** * * @param date * date */ public void setdate(string date) { this.date = date; } /** * * @return * timestamp */ public string gettimestamp() { return timestamp; } /** * * @param timestamp * timestamp */ public void settimestamp(string timestamp) { this.timestamp = timestamp; } /** * * @return * title */ public string gettitle() { return title; } /** * * @param title * title */ public void settitle(string title) { this.title = title; } /** * * @return * subject */ public string getsubject() { return subject; } /** * * @param subject * subject */ public void setsubject(string subject) { this.subject = subject; } }
jsonresponsefeed.js:
import java.util.arraylist; public class jsonresponsefeed { private arraylist<feedelement> feeds; /** * @return feeds */ public arraylist<feedelement> getfeed() { return feeds; } }
requestinterfacefeed.js:
import retrofit2.call; import retrofit2.http.get; import retrofit2.http.query; public interface requestinterfacefeed { @get("getfeed") call<jsonresponsefeed> getjson(@query("key") string apikey); }
dataadapterfeed.js:
import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.util.list; public class dataadapterfeed extends recyclerview.adapter<dataadapterfeed.viewholder> { private list<feedelement> feed; public dataadapterfeed(list<feedelement> feed) { this.feed = feed; } @override public dataadapterfeed.viewholder oncreateviewholder(viewgroup viewgroup, int i) { view view = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.card_row, viewgroup, false); return new viewholder(view); } @override public void onbindviewholder(dataadapterfeed.viewholder viewholder, int i) { viewholder.tv_name.settext(feed.get(i).getusername()); } @override public int getitemcount() { return feed.size(); } public class viewholder extends recyclerview.viewholder{ private textview tv_name; public viewholder(view view) { super(view); tv_name = (textview)view.findviewbyid(r.id.username); } } }
functions mainactivity.js:
private void loadjson(){ final string url = "https://mydomainiscorrect.justreplacedithere/api/v1/"; retrofit retrofit = new retrofit.builder() .baseurl(url) .addconverterfactory(gsonconverterfactory.create()) .build(); final requestinterfacefeed request = retrofit.create(requestinterfacefeed.class); call<jsonresponsefeed> call = request.getjson(apikey); call.enqueue(new callback<jsonresponsefeed>() { @override public void onresponse(call<jsonresponsefeed> call, retrofit2.response<jsonresponsefeed> response) { list<feedelement> data = response.body().getfeed(); adapter = new dataadapterfeed(data); recyclerview.setadapter(adapter); } @override public void onfailure(call<jsonresponsefeed> call, throwable t) { log.d("error", t.getmessage()); } }); } private void initviews(){ recyclerview = (recyclerview)findviewbyid(r.id.card_recycler_view); recyclerview.sethasfixedsize(true); recyclerview.layoutmanager layoutmanager = new linearlayoutmanager(getapplicationcontext()); recyclerview.setlayoutmanager(layoutmanager); loadjson(); }
if needed - cardview.xml:
<android.support.v7.widget.cardview xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="5dp" android:layout_marginleft="5dp" android:layout_marginright="5dp" card_view:cardcornerradius="5dp"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:orientation="vertical"> <textview android:id="@+id/username" android:layout_margintop="10dp" android:layout_marginbottom="10dp" android:textsize="18sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textstyle="bold" /> </linearlayout>
mainactivity recyclerview xml:
<android.support.v7.widget.recyclerview android:id="@+id/card_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/>
i'm stil pretty new java , android programming, know php pretty good, java gives me headaches sometimes.
thank lot trying me, appreciate it!
jsonresponsefeed not required. update requestinterfacefeed
public interface requestinterfacefeed { @get("getfeed") call<list<feedelement>> getjson(@query("key") string apikey); }
and update loadjson method
private void loadjson(){ final string url = "https://mydomainiscorrect.justreplacedithere/api/v1/"; retrofit retrofit = new retrofit.builder() .baseurl(url) .addconverterfactory(gsonconverterfactory.create()) .build(); final requestinterfacefeed request = retrofit.create(requestinterfacefeed.class); call<list<feedelement>> call = request.getjson(apikey); call.enqueue(new callback<list<feedelement>>() { @override public void onresponse(call<list<feedelement>> call, retrofit2.response<list<feedelement>> response) { list<feedelement> data = response.body(); adapter = new dataadapterfeed(data); recyclerview.setadapter(adapter); } @override public void onfailure(call<list<feedelement>> call, throwable t) { log.d("error", t.getmessage()); } }); }
Comments
Post a Comment