0

Am trying to create a request to youtube and trying to parse the json-c result i get. The code is not working and is throwing exception. Where am i going wrong? Kindly help

MainActivity.java

    try{
        HttpClient client = new DefaultHttpClient();

        HttpUriRequest request = new HttpGet("https://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&category=autos&max-results=1&orderby=relevance&q="+searchTerm);

        HttpResponse response = client.execute(request);

        String jsonString = StreamUtils.convertToString(response.getEntity().getContent());

        JSONObject json = new JSONObject(jsonString);

        JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");

        JSONObject jsonObject = jsonArray.getJSONObject(0);

        String title = jsonObject.getString("title");
        Toast.makeText(getApplicationContext(), title, Toast.LENGTH_LONG).show();
        }
        catch(Exception e)
        {
            Log.d("YOUTUBE", e.getMessage());


        }

StreamUtils.java

     public class StreamUtils {
  public static String convertToString(InputStream inputStream) throws IOException     {
    if (inputStream != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 1024);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            inputStream.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

}

OUTPUT FROM LOGCAT

01-06 21:15:00.328: E/AndroidRuntime(810): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.example.allinonedata/com.example.allinonedata.MainActivity}:   java.lang.NullPointerException: println needs a message
 01-06 21:15:00.328: E/AndroidRuntime(810):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
dnivra
  • 749
  • 4
  • 12
  • 30

0 Answers0