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 返回一个用字符串填充的 arrayList。

这不起作用。日志猫说列表中没有任何内容,但是在控制台中运行此程序会输出 6 个字符串。我对异步任务做错了吗?

我认为 onPostExecute 在列表被填充之前正在运行。

我知道我应该返回一个我将要使用的结果,但我现在只是在调试。

整个程序按要求。

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)

编辑

我相信,这段代码返回 null。下面提到,android 并不支持 JVM 的所有 future。这里有什么问题,应该如何解决?

 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();
 }

权限也不是问题。我添加了每一个权限,它仍然会引发错误。

4

3 回答 3

0

因为您没有记录您的 try/catch 中是否引发了异常,所以我无法确定,但我怀疑您没有为您的应用程序访问互联网请求适当的权限。你必须添加这个:

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

到您的 AndroidManifest.xml 文件,以便从您的应用程序进行任何基于 Internet 的调用。如果这仍然不起作用,请在程序的 catch 块中添加某种日志语句,因为我怀疑您的调用实际上失败了。请注意,仅仅因为您的 snippit 从 main 方法运行,它并不能保证它将在 android 上运行。虽然 android 支持标准 JVM 的几乎所有功能,但它们并不是 100% 的功能等效。

于 2013-06-19T05:10:21.760 回答
0

获取 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;

这是在android中获取html的解决方案。

Android似乎不喜欢这样:

 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();
 }

任何理由将不胜感激。谢谢你。

如何从android中的html链接获取页面的html源?

于 2013-06-19T21:21:52.480 回答
0

检查每个组件是否myUrl.getAvailabledyoutubedownloads().get(0).getDladdress()为空,并检查它showDownloadLinks是否为空。

于 2013-06-19T05:33:21.323 回答