Android Can u anyone please show a sample for... (Android) 1) How to get a data from a URL response using JSON? 2) How to parse that response to append in another URL?
2 Answers
If you understand german, this is a great tutorial:
http://andforge.net/2010/serverkommunikation-android-rest-json/
But in english there are also good tutorials, like .e.g. this two:
http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/
http://www.ibm.com/developerworks/web/library/x-andbene1/?ca=drs-
Comments
String url = "http://www.xyz.com/services/json";
String line,response;
conn=(HttpURLConnection)(new URL(url)).openConnection();
conn.setDoOutput(true);
wr=new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
rd=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = "";
while((line=rd.readLine())!=null)
{
response=response+line;
}
wr.close();
rd.close();
obj = new JSONObject(response);
Now you can use different functions like obj.getString, obj.getJSONArray, obj.getJSONObject etc to parse the object.
3 Comments
Jhon
Thankyou prateek, can i ask u something specifically
Jhon
I have created a login screen and written like If i press login button, it need to connect to an URL, it returning a message in webview that status code = 0, with session key. Means its successfully logged in. Now i need to get the response, check that status code if == 0; then move it to next screen, when i am moving to next screen it need to append that session key in next screens URL. all this using JSON.. do you have any idea?
Prateek Jain
Jhon can you post the "obj" value you are getting after hitting the URL? Then only I can answer how to extract the session id from it and then append it to the other URL.