Android JSON POST and then response -


i can retrieve data through json in listview want show data relative user logged in have post username php script. don't have idea how post username php script , respond web server.

private class getfixture extends asynctask<void, void, void> {      @override     protected void onpreexecute() {         super.onpreexecute();     }     @override     protected void doinbackground(void... arg) {           servicehandler serviceclient = new servicehandler();         log.d("url: ", "> " + url_items);         string json = serviceclient.makeservicecall(url_items,servicehandler.get);         // print json response in log         log.d("get match fixture resps","> " + json);         if (json != null) {             try {                 log.d("try", "in try");                 jsonobject jsonobj = new jsonobject(json);                 log.d("jsonobject", "new json object");                 // getting json array node                 matchfixture = jsonobj.getjsonarray(tag_fixture);                 log.d("json aray", "user point array");                 int len = matchfixture.length();                 log.d("len", "get array length");                 (int = 0; < matchfixture.length(); i++) {                     jsonobject c = matchfixture.getjsonobject(i);                     string matchid = c.getstring(tag_matchid);                     log.d("matchid", matchid);                     string teama = c.getstring(tag_teama);                     log.d("teama", teama);                     string teamb = c.getstring(tag_teamb);                     log.d("teamb", teamb);                     //  hashmap single match                     hashmap<string, string> matchfixture = new hashmap<string, string>();                     // adding each child node hashmap key => value                     matchfixture.put(tag_matchid, matchid);                     matchfixture.put(tag_teama, teama);                     matchfixture.put(tag_teamb, teamb);                     matchfixturelist.add(matchfixture);                 }             }             catch (jsonexception e) {                 log.d("catch", "in catch");                 e.printstacktrace();             }         } else {             log.e("json data", "didn't receive data server!");         }         return null;     }       @override     protected void onpostexecute(void result) {         super.onpostexecute(result);         listadapter adapter = new simpleadapter(                 doctor_names.this, matchfixturelist,                 r.layout.list_item, new string[] {                 tag_matchid, tag_teama,tag_teamb         }                 , new int[] {                 r.id.teama,r.id.name,                 r.id.teamb         }         );         setlistadapter(adapter);      } 

sir code show me data server want specific data want show data related person logged in have pass user name php script dont have idea post thing web on basis of can filter data

here example:

jsonobject obj = new jsonobject(); obj.put("username", "youruser"); httpurlconnectionjson.sendhttpdata("http://" + serveraddress + "/api/someusermethod", obj); 

httpurlconnectionjson class

public class httpurlconnectionjson {      private static final string tag = "httpurlconnectionjson";      public static string sendhttpdata(string urlpath, jsonobject json) {         httpurlconnection connection = null;         try {             url url=new url(urlpath);             connection = (httpurlconnection) url.openconnection();             connection.setdooutput(true);             connection.setdoinput(true);             connection.setrequestmethod("post");             connection.setrequestproperty("content-type", "application/json");             connection.setrequestproperty("accept", "application/json");             outputstreamwriter streamwriter = new outputstreamwriter(connection.getoutputstream());             streamwriter.write(json.tostring());             streamwriter.flush();             stringbuilder stringbuilder = new stringbuilder();             if (connection.getresponsecode() == httpurlconnection.http_ok){                 inputstreamreader streamreader = new inputstreamreader(connection.getinputstream());                 bufferedreader bufferedreader = new bufferedreader(streamreader);                 string response = null;                 while ((response = bufferedreader.readline()) != null) {                     stringbuilder.append(response + "\n");                 }                 bufferedreader.close();                  log.d(tag, stringbuilder.tostring());                 return stringbuilder.tostring();             } else {                 log.e(tag, connection.getresponsemessage());                 return null;             }         } catch (exception exception){             log.e(tag, exception.tostring());             return null;         } {             if (connection != null){                 connection.disconnect();             }         }     } } 

i hope help


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -