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();
}
权限也不是问题。我添加了每一个权限,它仍然会引发错误。