JSON Array & Parse Response
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 t开发者_StackOverflow社区hat response to append in another URL?
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-
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.
精彩评论