android - display listview of files available on any files hosting server in app -
i working on android app show files available on file hosting service in listview can download them. okay file hosting service if works me. have tried dropbox not working me.please suggest me code or regarding topic. have tried apache service didn't work. thank you!!!
package com.example.mangesh.comp; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.os.asynctask; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.widget.arrayadapter; import android.widget.listview; import android.widget.toast; import java.util.arraylist; import java.util.list; public class ass_bce extends actionbaractivity { dlfiles clientserverinterface = new dlfiles(); private string localjsonstring="{\"data\":[{\"file_name\": \"file.pdf\", \"physical_path\": \"/pic/file.png\"}]"; private listview listview; private list list; @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ass_bce); listview = (listview) findviewbyid(r.id.listview1); new retreivedata().execute(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_ass_bce, menu); return true; } class retreivedata extends asynctask<string,void,list<string>> { @override protected list<string> doinbackground(string... strings) { // todo auto-generated method stub try{ jsonobject jobj = clientserverinterface.makehttprequest("localhost/man.php"); jsonarray jsonarray = new jsonarray(localjsonstring); list = new arraylist<string>(); if (jsonarray != null) { int len = jsonarray.length(); (int i=0;i<len;i++) { list.add(jsonarray.get(i).tostring()); } } else toast.maketext(getapplicationcontext(),"array null",toast.length_long).show(); } catch (jsonexception e) { toast.maketext(getapplicationcontext(),"error"+e.tostring(),toast.length_long).show(); } return list; } protected void onpostexecute(list<string> list) { arrayadapter<string> aa = new arrayadapter<string>(getapplicationcontext(), android.r.layout.simple_list_item_1, list); listview.setadapter(aa); } } }
you can make simple api take request android app , return response in json containing files , path.
for example,
php snippet,
if (isset($_post['tag']) && $_post['tag'] != '') { // tag $tag = $_post['tag']; // check tag type if ($tag == 'get_data') { // print data $user = $db->getdata(); $response["error"] = false; $response["data"]["file_name"] = $user["file_name"]; $response["data"]["physical_path"] = $user["physical_path"]; echo json_encode($response); } }
at http://anyhost.com/api/api.php accepts post request , responses following json,
{ "data": [ { "file_name": "file.png", "physical_path": "/pic/file.png" }, { "file_name": "file.png", "physical_path": "/pic/file.png" } ] }
now parse json , show in app. see example here,
Comments
Post a Comment