android - How To Create AppWidget to display JSON Parse Images and Text -
i'm new android development , don't understand how create appwidget application parse json data , display in list.
i solved problem using link (https://laaptu.wordpress.com/2013/07/19/android-app-widget-with-listview/).
it has series of tutorials (1.app widget listview 2.populate app widget listview data web 3.download images , show on imageview of appwidget listview 4.setting update interval on appwidget listview 5.how make appwidget update work after phone reboot)
to use simple json url fetch images , texts, made following changes in remotefetchservice.java third tutorial,
import java.util.arraylist; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.service; import android.appwidget.appwidgetmanager; import android.content.intent; import android.graphics.bitmap; import android.os.asynctask; import android.os.ibinder; import android.util.log; import com.androidquery.aquery; import com.androidquery.callback.ajaxcallback; import com.androidquery.callback.ajaxstatus; import com.example.mk.widgets.data.databasemanager; import com.example.mk.widgets.data.filemanager; public class remotefetchservice extends service { private int appwidgetid = appwidgetmanager.invalid_appwidget_id; jsonobject jsonobject; jsonarray jsonarray; aquery aquery; private string remotejsonurl = "http://microblogging.wingnity.com/jsonparsingtutorial/jsonactors"; public static arraylist<listitem> listitemlist; private int count = 0; @override public ibinder onbind(intent arg0) { return null; } /* * retrieve appwidget id intent needed update widget later * initialize our aquery class */ @override public int onstartcommand(intent intent, int flags, int startid) { if (intent.hasextra(appwidgetmanager.extra_appwidget_id)) appwidgetid = intent.getintextra( appwidgetmanager.extra_appwidget_id, appwidgetmanager.invalid_appwidget_id); aquery = new aquery(getbasecontext()); new downloadjson().execute(); return super.onstartcommand(intent, flags, startid); } // downloadjson asynctask private class downloadjson extends asynctask<void, void, void> { @override protected void doinbackground(void... params) { // create array listitemlist = new arraylist<listitem>(); // retrieve json objects given url address jsonobject = jsonfunctions.getjsonfromurl(remotejsonurl); try { // locate array name in json jsonarray = jsonobject.getjsonarray("actors"); (int = 0; < jsonarray.length(); i++) { jsonobject = jsonarray.getjsonobject(i); // retrive json objects listitem listitem = new listitem(); listitem.heading = jsonobject.getstring("name"); listitem.content = jsonobject.getstring("country"); listitem.imageurl = jsonobject.getstring("image"); listitemlist.add(listitem); } storelistitem(); } catch (jsonexception e) { log.e("error", e.getmessage()); e.printstacktrace(); } return null; } } /** * instead of using static arraylist have used before,no rely upon * data stored on database saving fetched json file content * database , @ same time downloading image web */ private void storelistitem() { databasemanager dbmanager = databasemanager.instance; dbmanager.init(getbasecontext()); dbmanager.storelistitems(appwidgetid, listitemlist); int length = listitemlist.size(); (int = 0; < length; i++) { listitem listitem = listitemlist.get(i); final int index = i; aquery.ajax(listitem.imageurl, bitmap.class,new ajaxcallback<bitmap>() { @override public void callback(string url, bitmap bitmap, ajaxstatus status) { super.callback(url, bitmap, status); storebitmap(index, bitmap); }; }); } } /** * saving downloaded images file , after download of * images complete begin populate widget done */ private void storebitmap(int index, bitmap bitmap) { filemanager.instance.storebitmap(appwidgetid, bitmap, listitemlist.get(index).heading, getbasecontext()); count++; log.i("count",string.valueof(count) + "::"+ integer.tostring(listitemlist.size())); if (count == listitemlist.size()) { count = 0; populatewidget(); } } /** * method sends broadcast widgetprovider widget notified * necessary action , here action == widgetprovider.data_fetched */ private void populatewidget() { intent widgetupdateintent = new intent(); widgetupdateintent.setaction(widgetprovider.data_fetched); widgetupdateintent.putextra(appwidgetmanager.extra_appwidget_id, appwidgetid); sendbroadcast(widgetupdateintent); this.stopself(); } }
jsonfunctions.java
import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; public class jsonfunctions { public static jsonobject getjsonfromurl(string url) { inputstream = null; string result = ""; jsonobject jarray = null; // download json data url try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection " + e.tostring()); } // convert response string try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); } catch (exception e) { log.e("log_tag", "error converting result " + e.tostring()); } try { jarray = new jsonobject(result); } catch (jsonexception e) { log.e("log_tag", "error parsing data " + e.tostring()); } return jarray; } }
hope helps , great ( https://stackoverflow.com/users/739306/laaptu ) tutorials.
Comments
Post a Comment