mainactivity.java
package com.softjourn.redmineclient.activities; import android.os.asynctask; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.base64; import android.view.menu; import android.view.menuitem; import android.widget.arrayadapter; import android.widget.listview; import com.google.gson.gson; import com.google.gson.jsonarray; import com.google.gson.jsonelement; import com.google.gson.jsonparser; import com.softjourn.redmineclient.r; import com.softjourn.redmineclient.models.issues; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.protocolexception; import java.net.url; import java.util.arraylist; import butterknife.bind; import butterknife.butterknife; public class mainactivity extends appcompatactivity { private final static string uri="https://redmineng.softjourn.if.ua/issues.json?assigned_to_id=me"; @bind(r.id.list_issues) listview mlistissues; login li; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.bind(this); li = new login(); li.execute(uri); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } class login extends asynctask<string, void, string> implements com.softjourn.redmineclient.activities.login { @override protected string doinbackground(string... strings) { httpurlconnection c = null; try { c = (httpurlconnection) new url(strings[0]).openconnection(); } catch (ioexception e) { e.printstacktrace(); } c.setusecaches(false); try { c.setrequestmethod("get"); } catch (protocolexception e) { e.printstacktrace(); } c.setrequestproperty("accept", "application/json"); c.setrequestproperty("authorization", "basic " + base64.encodetostring("osavchak:somepassword".getbytes(), 0)); try { c.connect(); } catch (ioexception e) { e.printstacktrace(); } bufferedreader br = null; try { br = new bufferedreader(new inputstreamreader((c.getinputstream()))); } catch (ioexception e) { e.printstacktrace(); } stringbuilder sb = new stringbuilder(); string output; try { while ((output = br.readline()) != null) { sb.append(output); } } catch (ioexception e) { e.printstacktrace(); } return sb.tostring(); } @override protected void onpostexecute(string result) { super.onpostexecute(result); jsonarray ja = new jsonparser().parse(result).getasjsonobject().getasjsonarray("issues"); arraylist<issues> issues = new arraylist<issues>(); for(jsonelement jo : ja) { issues issue = new gson().fromjson(jo, issues.class); issues.add(issue); } arrayadapter<issues> adapter = new arrayadapter<issues>(mainactivity.this, android.r.layout.simple_list_item_1, issues); mlistissues.setadapter(adapter); } } } issues.java
package com.softjourn.redmineclient.models; import com.google.gson.annotations.serializedname; import java.util.arraylist; public class issues extends arraylist<issues> { @serializedname("id") private int id; @serializedname("description") private string description; public int getid() { return id; } public string getdescription() { return description; } public issues(int id, string description) { this.id = id; this.description = description; } @override public string tostring() { return "[" + this.id + "] " + this.description; } } the json response looks like:
{"issues":[{"id":11023,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":10,"name":"day off"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-06-08","due_date":"2015-06-08","done_ratio":0,"created_on":"2015-06-04t19:12:14z","updated_on":"2015-06-05t08:43:35z"},{"id":10994,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":12,"name":"medical"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-06-03","due_date":"2015-06-03","done_ratio":0,"created_on":"2015-06-03t07:19:01z","updated_on":"2015-06-03t08:09:21z"},{"id":10651,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":11,"name":"business trip"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"u.s. visa development.","start_date":"2015-04-20","due_date":"2015-04-20","done_ratio":0,"created_on":"2015-04-09t15:58:38z","updated_on":"2015-04-10t07:39:38z"},{"id":10555,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":10,"name":"day off"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-05-05","due_date":"2015-05-05","done_ratio":0,"created_on":"2015-04-03t10:24:11z","updated_on":"2015-04-08t08:18:13z"},{"id":10554,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":10,"name":"day off"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-04-30","due_date":"2015-04-30","done_ratio":0,"created_on":"2015-04-03t10:19:40z","updated_on":"2015-04-08t08:18:12z"},{"id":10219,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":10,"name":"day off"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"czech visa development","start_date":"2015-03-23","due_date":"2015-03-23","done_ratio":0,"created_on":"2015-03-06t08:59:47z","updated_on":"2015-04-06t09:20:55z"},{"id":10199,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":12,"name":"medical"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-03-04","due_date":"2015-03-04","done_ratio":0,"created_on":"2015-03-03t17:35:48z","updated_on":"2015-03-04t09:27:02z"},{"id":10183,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":12,"name":"medical"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-03-03","due_date":"2015-03-03","done_ratio":0,"created_on":"2015-03-02t14:27:40z","updated_on":"2015-03-02t14:34:57z"},{"id":9791,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":10,"name":"day off"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2015-01-09","due_date":"2015-01-09","done_ratio":0,"created_on":"2015-01-06t14:09:14z","updated_on":"2015-01-08t11:54:55z"},{"id":9128,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":9,"name":"vacation"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"vacation","start_date":"2014-11-03","due_date":"2014-11-14","done_ratio":0,"created_on":"2014-10-15t12:28:33z","updated_on":"2014-10-16t09:17:31z"},{"id":7921,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":9,"name":"vacation"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"vacation 2 weeks","start_date":"2014-08-11","due_date":"2014-08-21","done_ratio":0,"created_on":"2014-06-03t14:04:15z","updated_on":"2014-06-05t10:01:23z"},{"id":7698,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":12,"name":"medical"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"medical on monday","start_date":"2014-05-12","due_date":"2014-05-12","done_ratio":0,"created_on":"2014-05-11t10:18:58z","updated_on":"2014-05-13t08:21:08z"},{"id":7337,"project":{"id":52,"name":"our vacations , days off"},"tracker":{"id":10,"name":"day off"},"status":{"id":12,"name":"accepted"},"priority":{"id":1,"name":"normal"},"author":{"id":186,"name":"savchak orest"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"orest savchak","description":"","start_date":"2014-02-27","due_date":"2014-02-28","done_ratio":0,"created_on":"2014-02-11t12:34:16z","updated_on":"2014-02-17t15:15:30z"},{"id":6823,"project":{"id":138,"name":"orest savchak tasks"},"tracker":{"id":2,"name":"user story"},"status":{"id":1,"name":"new"},"priority":{"id":1,"name":"normal"},"author":{"id":42,"name":"vershynin yevgeniy"},"assigned_to":{"id":186,"name":"savchak orest"},"subject":"authomatization testing","description":"testing imax android app.","start_date":"2013-12-04","done_ratio":0,"created_on":"2013-12-04t16:16:16z","updated_on":"2014-01-21t16:29:49z","story_points":null}],"total_count":14,"offset":0,"limit":25} and when run application crashes 'com.google.gson.jsonsyntaxexception: java.lang.illegalstateexception: expected begin_array begin_object'
how can fix it?
the problem you're telling gson have object of type when in fact have array of objects of type.
try way instead:
type collectiontype = new typetoken < list<issues>>(){}.gettype(); list<issues> issues = (list<issues>) new gson().fromjson( json_response, collectiontype);
Comments
Post a Comment