java - Android Login input to be send via HttpGet JSON to server and return list of user profile information -
i have login window, want user information next window , share username. both stored password , username in database , want array result. here code. 2 edittext have values , pass them via json. link request ..../userprofiles?username=. so, when type username have receive basic info. in json this:
{ "content": [ { "id": 4, "email": "email@domain.com", "password": "this_will_not_be_set", "firstname": "john", "phonenumber": "11223344", "username": "newuser", "birthdate": 1394204315000, "gender": "male", "blacklisteddate": null, "userpicture": "oik=", "blacklisted": false }], "size": 25, "number": 0, "sort": null, "totalpages": 1, "numberofelements": 1, "totalelements": 1, "lastpage": true, "firstpage": true }
and android code is:
public static string get(string url) { inputstream inputstream = null; string result = ""; try { httpclient httpclient = new defaulthttpclient(); // make request give url httpresponse httpresponse = httpclient.execute(new httpget(url)); // receive response inputstream inputstream = httpresponse.getentity().getcontent(); // convert inputstream string if (inputstream != null) result = convertinputstreamtostring(inputstream); else result = "did not work!"; } catch (exception e) { log.d("inputstream", e.getlocalizedmessage()); } return result; } private class httpasynctask extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { return get(urls[0]); } @override protected void onpostexecute(string result) { toast.maketext(getbasecontext(), "received", toast.length_long) .show(); try { jsonobject json = new jsonobject(result); string str = ""; jsonarray jarr = json.getjsonarray("content"); str += "username: " + jarr.getjsonobject(0).getstring("username"); str += "password: " + jarr.getjsonobject(0).getstring("password"); usernameedittext.settext(str); passwordedittext.settext(str); //etresponse.settext(json.tostring(1)); } catch (jsonexception e) { e.printstacktrace(); } } }
any appreciated! tnx in advance!
jumbojey
you can use following code result http connection
download follwoing jar file link
https://github.com/loopj/android-async-http/raw/master/releases/android-async-http-1.4.4.jar
put jar lib folder , add follwing code
import com.loopj.android.http.*; asynchttpclient client = new asynchttpclient(); client.get("your url", new asynchttpresponsehandler() { @override public void onsuccess(string response) { system.out.println(response); try { jsonobject json = new jsonobject(result); jsonarray jarr = json.getjsonarray("content"); if(jarr.length>0) { jsonobject jobj =jarr.getjsonobject(0); string username=jobj.getstring("username"); string pass=jobj.getstring("password"); usernameedittext.settext(str); passwordedittext.settext(str); } } catch (jsonexception e) { e.printstacktrace(); } });
Comments
Post a Comment