2013-12-14 4 views
0

나는 응용 프로그램이 중지되었지만 오류가 발생하지 않았습니다. YouTube 제목에서 arraylist를 채우고 싶습니다 .JTube 데이터를 구문 분석하고 싶지만 프로그램이 중지되었습니다. 어떻게 해결할 수 있습니까? 뭔가 잘못 됐어? 왜 jsonc를 사용하는youtube Json - C error

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'가 출력되어 있습니까? – Hariharan

+0

@ Tamilan 아니요, 활동이 실행될 때 앱이 중지됩니다. –

+0

오류가 발생하지 않습니다. 로그에 아무 것도 출력하지 않습니다. 여전히 안다면 앱을 다시 실행하십시오. 프로젝트를 청소하고 실행하십시오. – Hariharan

답변

0

...이 제공하지 않는 새 YouTube API를이다 당신은 당신이 원하는 모든 정보와 태그가 미래에있을 수 있습니다. 나는 json 형식을 사용해야한다. gson 라이브러리를 사용하여 파싱 할 수도 있습니다.

+0

어떻게 gson이 URL을 구문 분석 할 수 있습니까? 제발 약간의 코드를주세요. –

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 responce에 대한

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 클래스. 너는 갈 수있어.