开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜