2013-12-14 44 views
0

我没有得到任何错误,但应用程序停止。 我想从YouTube标题中填入数组列表。我想从YouTube上解析Json数据,但程序已停止。 我该如何解决这个问题?哪里错了?youtube Json - C错误

feedUrl="https://gdata.youtube.com/feeds/api/users/muyap/uploads?v=2&alt=jsonc&max-results=2"; 

HttpClient client = new DefaultHttpClient(); 
HttpGet getRequest = new HttpGet(feedUrl); 
HttpResponse responce; 
try { 
    responce = client.execute(getRequest); 
    StatusLine statusLine = responce.getStatusLine(); 
    int statusCode = statusLine.getStatusCode(); 
    if(statusCode!=200) 
    { 

     Toast.makeText(YouTube.this, "Yükleme Gerçekleşmedi", Toast.LENGTH_LONG).show(); 

    }else{ 
    InputStream JsonStream=responce.getEntity().getContent(); 
    BufferedReader reader= new BufferedReader(new InputStreamReader(JsonStream)); 
    StringBuilder builder = new StringBuilder(); 
    String line; 
    while((line=reader.readLine())!=null) 
    { 

     builder.append(line); 
    } 
    String JSONdata = builder.toString(); 
    Log.i("JsonData",JSONdata); 
    JSONObject json = new JSONObject(JSONdata); 
    JSONObject data = json.getJSONObject("data"); 
    JSONArray items=data.getJSONArray("items"); 

    for(int i=0;i<items.length();i++) 
    { 

     JSONObject video=items.getJSONObject(i); 
     videoArrayList.add(video.getString("title")); 

    } 

    list = (ListView)findViewById(R.id.listView1); 
    list.setAdapter(new ArrayAdapter<String>(YouTube.this, android.R.layout.simple_list_item_1,videoArrayList)); 

    } 
} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

First Log Page

Second Log Page

我想显示日志1和2

+0

您的'JSONdata'是否打印在'Log'中? – Hariharan

+0

@Tamilan不,应用程序在活动运行时停止 –

+0

您没有错误。没有在日志中打印。如果还没有收到,再次运行应用程序。尝试清理该项目并运行。 – Hariharan

回答

0

为什么使用jsonc ......这是新的YouTube API,它不提供你所有的信息和标签,你想要的可能在未来。我已经使用它,你应该使用json格式。这也可以使用gson库解析它。

+0

我该如何解析与gson这个网址?请给我一些代码 –

0

我的回答试试这个..

的OnCreate()

feedUrl="https://gdata.youtube.com/feeds/api/users/muyap/uploads?v=2&alt=jsonc&max-results=2"; 

list = (ListView)findViewById(R.id.listView1); 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
     new JsonParsing(feedUrl).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{null}); 
else 
    new JsonParsing(feedUrl).execute(new String[]{null}); 

JsonParsing

public class JsonParsing extends AsyncTask<String, Void, ArrayList<String>> { 

      // variables passed in: 
      String urls; 
      ProgressDialog pDialog; 
      // constructor 
      public JsonParsing(String urls) { 
       this.urls = urls; 
      } 

      @Override 
      protected void onPreExecute() { 
       pDialog = ProgressDialog.show(NetActivity.this, "Fetching Details..", "Please wait...", true); 
      } 


      @Override 
      protected ArrayList<String> doInBackground(String... params) { 
       // TODO Auto-generated method stub 

       HttpClient client = new DefaultHttpClient(); 
HttpGet getRequest = new HttpGet(feedUrl); 
HttpResponse responce; 
try { 
    responce = client.execute(getRequest); 
    StatusLine statusLine = responce.getStatusLine(); 
    int statusCode = statusLine.getStatusCode(); 
    if(statusCode!=200) 
    { 

     Toast.makeText(YouTube.this, "Yükleme Gerçekleşmedi", Toast.LENGTH_LONG).show(); 

    }else{ 
    InputStream JsonStream=responce.getEntity().getContent(); 
    BufferedReader reader= new BufferedReader(new InputStreamReader(JsonStream)); 
    StringBuilder builder = new StringBuilder(); 
    String line; 
    while((line=reader.readLine())!=null) 
    { 

     builder.append(line); 
    } 
    String JSONdata = builder.toString(); 
    Log.i("JsonData",JSONdata); 
    JSONObject json = new JSONObject(JSONdata); 
    JSONObject data = json.getJSONObject("data"); 
    JSONArray items=data.getJSONArray("items"); 

    for(int i=0;i<items.length();i++) 
    { 

     JSONObject video=items.getJSONObject(i); 
     videoArrayList.add(video.getString("title")); 

    } 
} 
} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

       return videoArrayList; 
      } 

      @Override 
      protected void onPostExecute(ArrayList<String> result) { 
       // Now we have your JSONObject, play around with it. 
       if (pDialog.isShowing()) 
         pDialog.dismiss(); 

       list.setAdapter(new ArrayAdapter<String>(YouTube.this, android.R.layout.simple_list_item_1,result)); 

      } 

     } 
0
public class YouTubeClient { 
    // If you are building an Android application that needs to work with all 
    // Android 
    // SDKs, simply call AndroidHttp.newCompatibleTransport() and it will decide 
    // of 
    // these two to use based on the Android SDK level. 
    private final HttpTransport transport = new NetHttpTransport(); 

    private final HttpRequestFactory requestFactory; 

    private static YouTubeClient youTubeClient; 

    public YouTubeClient() { 

     requestFactory = transport.createRequestFactory(new HttpRequestInitializer() { 

      @Override 
      public void initialize(HttpRequest request) { 
       // headers 
       GoogleHeaders headers = new GoogleHeaders(); 
       headers.setApplicationName("YouTubeSample/1.0"); 
       headers.setGDataVersion("2"); 
       request.setHeaders(headers); 
       request.setParser(new JsonObjectParser(new JacksonFactory())); 
      } 
     }); 

    } 

    public static YouTubeClient getInstance() { 
     if (youTubeClient == null) { 
      youTubeClient = new YouTubeClient(); 
     } 

     return youTubeClient; 
    } 

    public YoutubeGson executeGetFeed(GoogleUrl url) throws VidioException { 

//  "https://gdata.youtube.com/feeds/api/videos?category=Comedy&time=this_week"); 
     HttpRequest request; 
     HttpResponse response = null; 
     try { 
      request = requestFactory.buildGetRequest(url); 
      AppLog.i("url-----" + request.getUrl()); 
      response = request.execute(); 
     } catch (javax.net.ssl.SSLException e) { 
      AppLog.error("entry rejected " + e); 
      throw new VidioException(ErrorMessage.REJECTED); 
     } catch (Exception e) { 
//   Log.i(TAG, "error" + e); 
      AppLog.i("error ion execute--1111111--" + e); 
      throw new VidioException(ErrorMessage.CONNECTION); 
     } 
     return getGsonFromFeed(response); 

    } 

    public YoutubeGson getGsonFromFeed(HttpResponse response) throws VidioException { 

     YoutubeGson youtubeGson = null; 
     try { 
//   AppLog.i("in getfeed------" + response.parseAsString()); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getContent(), "UTF-8")); 
      Gson gson = new Gson(); 
      youtubeGson = gson.fromJson(reader, YoutubeGson.class); 
//   AppLog.i("----------------------" + youtubeGson.feed.entry); 

      // Log.i(TAG, "videoentry ----" + 
//   youtubeGson.feed.entry.get(0).media$group.yt$duration.seconds); 

//   AppLog.i("result----------" + youtubeGson.categories.get(0).category_name); 
     } catch (Exception e) { 
//   Log.i(TAG, "error in get feed---" + e); 
      AppLog.i("error in get feed---" + e); 
      throw new VidioException(ErrorMessage.GSON); 
     } 
     return youtubeGson; 

    } 

化妆POJO类的性反应。你不用走了。