1
private class GetLinks extends AsyncTask<String, Void, String> {
    private EasyYoutubeDownloader myUrl = null;
    private String dls = "";

    protected String doInBackground(String... urls) {
        // works

        try {
            myUrl = new EasyYoutubeDownloader(
                    "http://www.youtube.com/watch?v=jhFDyDgMVUI");
        } catch (Exception e) {

        }

        return "";

    }

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {


        showDownloadLinks.setText(myUrl.getAvailabledyoutubedownloads().get(0).getDladdress());

    }
}

getAvailableyoutubedownloads returns an arrayList filled with Strings.

This does not work. The log cat says there is nothing in the List, however running this program in the console outputs 6 Strings. Am I doing something wrong with the asynch task?

I think the onPostExecute is running before the list gets populated.

I know I should be returning a result that I am going to use but I am just debugging right now.

The whole program as requested.

import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private EditText youtubeUrl;
    private Button getDownloadUrls;
    private TextView showDownloadLinks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        youtubeUrl = (EditText) findViewById(R.id.editText1);
        youtubeUrl.setText("http://www.youtube.com/watch?v=jhFDyDgMVUI");
        getDownloadUrls = (Button) findViewById(R.id.button1);
        showDownloadLinks = (TextView) findViewById(R.id.textView1);
        getDownloadUrls.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                new GetLinks().execute(youtubeUrl.getText().toString());

            }
        });
    }

    private class GetLinks extends AsyncTask<String, Void, String> {
        private EasyYoutubeDownloader myUrl = null;
        private String dls = "";

        protected String doInBackground(String... urls) {
            // works

            try {
                myUrl = new EasyYoutubeDownloader(
                        "http://www.youtube.com/watch?v=jhFDyDgMVUI");
            } catch (Exception e) {

            }

            return "";

        }

        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {


            showDownloadLinks.setText(myUrl.getAvailabledyoutubedownloads().get(0).getDladdress());

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

    }
}

LOGCAT

06-18 22:33:08.940: E/Trace(14828): error opening trace file: Permission denied (13)
06-18 22:33:08.940: D/ActivityThread(14828): setTargetHeapUtilization:0.25
06-18 22:33:08.940: D/ActivityThread(14828): setTargetHeapIdealFree:8388608
06-18 22:33:08.940: D/ActivityThread(14828): setTargetHeapConcurrentStart:2097152
06-18 22:33:09.070: D/libEGL(14828): loaded /system/lib/egl/libEGL_adreno200.so
06-18 22:33:09.070: D/libEGL(14828): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
06-18 22:33:09.070: D/libEGL(14828): loaded /system/lib/egl/libGLESv2_adreno200.so
06-18 22:33:09.080: I/Adreno200-EGLSUB(14828): <ConfigWindowMatch:2087>: Format RGBA_8888.
06-18 22:33:09.090: E/(14828): <s3dReadConfigFile:75>: Can't open file for reading
06-18 22:33:09.100: E/(14828): <s3dReadConfigFile:75>: Can't open file for reading
06-18 22:33:09.100: D/OpenGLRenderer(14828): Enabling debug mode 0
06-18 22:33:16.848: D/AndroidRuntime(14828): Shutting down VM
06-18 22:33:16.848: W/dalvikvm(14828): threadid=1: thread exiting with uncaught exception (group=0x41bf0438)
06-18 22:33:16.858: E/AndroidRuntime(14828): FATAL EXCEPTION: main
06-18 22:33:16.858: E/AndroidRuntime(14828): java.lang.NullPointerException
06-18 22:33:16.858: E/AndroidRuntime(14828):    at com.simpleyoutube.download.MainActivity$GetLinks.onPostExecute(MainActivity.java:60)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at com.simpleyoutube.download.MainActivity$GetLinks.onPostExecute(MainActivity.java:1)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at android.os.AsyncTask.finish(AsyncTask.java:631)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at android.os.Looper.loop(Looper.java:137)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at android.app.ActivityThread.main(ActivityThread.java:5062)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
06-18 22:33:16.858: E/AndroidRuntime(14828):    at dalvik.system.NativeStart.main(Native Method)

EDIT

This piece of code is returning null, I believe. Mentioned below, android does not support all futures of JVM. What is wrong here, and how should one fix it?

 public static String getHtml(String url) throws Exception {
  URL website = new URL(url);
  URLConnection connection = website.openConnection();

  BufferedReader in = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));

  StringBuilder response = new StringBuilder();
  String inputLine;

  while ((inputLine = in.readLine()) != null)
   response.append(inputLine);

  in.close();

  return response.toString();
 }

Also permissions cannot be the problem. I added every single permission and it still throws error.

horvste
  • 636
  • 6
  • 19
  • What do you mean by running the program in the console? Also, can you post the rest of the program? `onPostExecute` will run once `doInBackground` has finished – verybadalloc Jun 19 '13 at 03:01
  • 1
    You should also be logging any caught exception, instead of just empty catch block. How do you know EasyYoutubeDownloader was created correctly. I would prefer that the EasyYoutube dowloader was your result or you at least returned a different result to indicate success or failure. – Lionel Port Jun 19 '13 at 03:04
  • I copy and pasted the classes to a java project and then System.out.println(myUrl.getAvailabledyoutubedownloads().get(0).getDladdress()) after running a main method. The android logcat is an eyesore to read. I know the EasyYoutubeDownloader was created correctly because If I run it in the console using a main method it prints out the desired output. – horvste Jun 19 '13 at 03:11
  • How do you plan to proceed with Android development in future if logcat is an eyesore to you? – Chor Wai Chun Jun 19 '13 at 03:18
  • It seems that when I need to quickly print "things" out that it is hard to find it in the logcat, probably due to user error. Using toasts is a nicer way to print "things" out quickly but it gets irritating to have to show a toast message each time. Any tips on reading the logcat would be welcome. – horvste Jun 19 '13 at 03:23
  • can you print the exception catched in doInBackground method? using e.printStackTrace , This can give you an understanding of the problem – Muhannad A.Alhariri Jun 19 '13 at 05:16
  • Error is null pointer exception from getHtml method as posted above – horvste Jun 19 '13 at 21:08

3 Answers3

0

Because you aren't logging whether or not an exception is being thrown in your try/catch, I can't know for sure, but I suspect that you have not requested the proper permissions for your app to access the internet. You must add this:

<uses-permission android:name="android.permission.INTERNET" /> 

to your AndroidManifest.xml file in order to make any internet based calls from your application. If this still doesn't work, please add some sort of log statement to the catch block in your program, since I suspect that your call is actually failing. Note that just because your snippit runs from a main method, it does not guarantee that it will run on android. While android supports almost all of the functionality that the standard JVM does, they are not 100% feature equivalent.

TwentyMiles
  • 4,063
  • 3
  • 30
  • 37
0

Check each component of myUrl.getAvailabledyoutubedownloads().get(0).getDladdress() for null, and check that showDownloadLinks is not null.

Ken
  • 30,811
  • 34
  • 116
  • 155
  • I realized myUrl wasn't directly causing the error but rather another empty arraylist in the other class was causing myUrl to be null because the getHtml code (posted above) is returning null. – horvste Jun 19 '13 at 21:08
0

SOLUTION FOR GETTING HTML

HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
         HttpGet httpget = new HttpGet(url); // Set the action you want to do
         HttpResponse response = httpclient.execute(httpget); // Executeit
         HttpEntity entity = response.getEntity(); 
         InputStream is = entity.getContent(); // Create an InputStream with the response
         BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
         StringBuilder sb = new StringBuilder();
         String line = null;
         while ((line = reader.readLine()) != null) // Read line by line
             sb.append(line + "\n");

         String resString = sb.toString(); // Result is here

         is.close(); // Close the stream
         return resString;

This is the solution, for getting html in android.

Android doesn't seem to like this:

 public static String getHtml(String url) throws Exception {
  URL website = new URL(url);
  URLConnection connection = website.openConnection();

  BufferedReader in = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));

  StringBuilder response = new StringBuilder();
  String inputLine;

  while ((inputLine = in.readLine()) != null)
   response.append(inputLine);

  in.close();

  return response.toString();
 }

Any reasons why would be appreciated. Thank You.

How to get the html-source of a page from a html link in android?

Community
  • 1
  • 1
horvste
  • 636
  • 6
  • 19