【问题标题】:how to get the output at required point in android program如何在android程序中的所需点获取输出
【发布时间】:2013-03-15 22:16:12
【问题描述】:

这是 JSON 文件:

{
   "response":{
      "code":1,
      "message":"success"
   },
   "data":{
      "updates":[
         {
            "update_id":"67",
            "date":"6 months ago",
            "update_type":"7",
            "update_id":"67",
            "name":"ravi"
         },
         {
            "update_id":"68",
            "date":"3 months ago",
            "update_type":"5",
            "update_id":"68",
            "name":"paresh"
         },
         {
            "update_id":"69",
            "date":"1 months ago",
            "update_type":"6",
            "update_id":"69",
            "name":"sampath"
         },
         {
            "update_id":"62",
            "date":"9 months ago",
            "update_type":"6",
            "update_id":"62",
            "name":"raju"
         }
      ]
   }
}

【问题讨论】:

    标签: android android-layout android-intent android-emulator android-widget


    【解决方案1】:

    试试这个,

     String str_json = "your json string from query";
    
        try {
            JSONObject topobj = new JSONObject(str_json);
            JSONObject innerobj = topobj.getJSONObject("yourdate");
    
            String name = innerobj.getString("yourdata");
            String photo = innerobj.getString("yourdata");
            JSONArray cuisines = innerobj.getJSONArray("yourdata");
            //etc etc...
    
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    【讨论】:

      【解决方案2】:
          try {
              HttpClient httpclient = getNewHttpClient();
              HttpGet httppost = new HttpGet(URL);
              HttpResponse response = httpclient.execute(httppost);
              HttpEntity entity = response.getEntity();
              is = entity.getContent();
      
          } catch(Exception e) {
              Log.e("log_tag", "Error in http connection " + e.toString());
          }
      
          // convert response to string
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
              is.close();
              result = sb.toString();
          } catch(Exception e) {
              Log.e("log_tag", "Error converting result " + e.toString());
          }
      
          try {
              Log.e("log_tag", "result" + result.toString());
              JSONObject json_data = new JSONObject(result);
              String data = json_data.getString("Data");
              json_data = new JSONObject(data);
              String updates = json_data.getString("updates");
              JSONArray json_data1 = new JSONArray(updates);
              for(int i = 0; i < json_data1.length(); i++) {
                  json_data = json_data1.getJSONObject(i);
                  String update_id = json_data.getString("update_id");
                  String date = json_data.getString("date");
              }
          } catch(Exception e) {
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-07
        • 1970-01-01
        • 1970-01-01
        • 2021-07-14
        • 2013-10-16
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        相关资源
        最近更新 更多