【问题标题】:Parse Simple Json response解析简单的 Json 响应
【发布时间】:2015-01-29 10:13:02
【问题描述】:

我正在尝试解析这个 Json 响应。出于某种奇怪的原因,它不起作用。请多多包涵 。我真的很讨厌 Json。

Here is the Url am trying to parse:

这是我用来解析它的代码:

      public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

       final String TAG = "AsyncTaskParseJson.java";

    // set your json string url here


    @Override
    protected void onPreExecute() {

        Toast.makeText(getActivity(), "started", Toast.LENGTH_SHORT).show();

    }

    @Override
    protected String doInBackground(String... arg0) {

        String str = "";
        HttpResponse response;
        HttpClient myClient = new DefaultHttpClient();
        HttpPost myConnection = new   HttpPost("http://gdata.youtube.com/feeds/api/videos/iS1g8G_njx8?v=2&alt=jsonc");

        try {
            response = myClient.execute(myConnection);
            str = EntityUtils.toString(response.getEntity(), "UTF-8");

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try{
            JSONObject myJson = new JSONObject(str);

            String grande =  myJson.getString("title");
            Toast.makeText(getActivity(), ""+grande, Toast.LENGTH_SHORT).show();


        } catch ( JSONException e) {
            e.printStackTrace();
        }



         return null;
      }

        @Override
        protected void onPostExecute(String strFromDoInBg) {

        Toast.makeText(getActivity(), "done", Toast.LENGTH_SHORT).show();

       }
   }

我只需要解析标题和描述。谢谢

【问题讨论】:

  • 你得到了什么异常
  • 你在解析过程中遇到了什么错误?

标签: java android json youtube-api


【解决方案1】:

看来你没有正确解析

使用以下代码更改您的尝试块

try{
            JSONObject myJson = new JSONObject(str);
            JSONObject entityObject = myJson.getJSONObject("entity");

            // Parsing title
            JSONOBject titleObject = entityObject.getJSONObject("title");
            String grande =  titleObject.getString("$t");

            // Do the same for Description as well (Like above two line)

            Toast.makeText(getActivity(), ""+grande, Toast.LENGTH_SHORT).show();


        } catch ( JSONException e) {
            e.printStackTrace();
   }

【讨论】:

    【解决方案2】:

    我认为您(在 doInBackground 中)的 toast 消息存在问题,请尝试将其注释掉,然后检查!

    此外,最佳实践是将响应作为参数发送到 onPostExecute,然后在 onPostExecute 中解析 Json

    【讨论】:

    • @Srinivas B 我是否正确解析了 Json?如果我确定我正确解析了它,那么我会尝试您的解决方案。
    【解决方案3】:

    试试这样,

    @Override
        protected String doInBackground(String... arg0) {
    
            String str = "";
            HttpResponse response;
            HttpClient myClient = new DefaultHttpClient();
            HttpPost myConnection = new   HttpPost("http://gdata.youtube.com/feeds/api/videos/iS1g8G_njx8?v=2&alt=jsonc");
    
            try {
                response = myClient.execute(myConnection);
                str = EntityUtils.toString(response.getEntity(), "UTF-8");
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
             return str;
          }
    
           @Override
            protected void onPostExecute(String result) {
    
            try{
                JSONObject myJson = new JSONObject(result);
    
                 JSONObject entry =  results.getJSONObject("entry");
                JSONObject grande =  results.getJSONObject("title");
                String title =  grande.getString("$t"); 
                Toast.makeText(getActivity(), title, Toast.LENGTH_SHORT).show();
    
    
            } catch ( JSONException e) {
                e.printStackTrace();
            }
    
            Toast.makeText(getActivity(), "done", Toast.LENGTH_SHORT).show();
    
           }
    

    【讨论】:

      猜你喜欢
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2019-11-17
      • 2013-03-27
      相关资源
      最近更新 更多