大家好,我遇到了这个奇怪的错误,我不知道为什么?
package com.androidbook.services.httpget;
import java.io.BufferedReader; import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
public class HttpGetDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://code.google.com/android/");
HttpResponse response = client.execute(request);
in = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); } finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
错误是这样的
Exception in thread "main" java.lang.RuntimeException: Stub!
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:5)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:7)
at ClientWithResponseHandler.main(ClientWithResponseHandler.java:15)`
最佳答案
您在这里导入了 Android SDK。您是否有机会尝试从您的电脑(例如,从标准 Java 项目)运行它?
此错误意味着您无权访问您尝试使用的实际方法或对象,而只能访问一个 stub - 该方法所做的只是抛出您看到的这个异常。
确保您在模拟器(或 Android 设备)上运行您的 Android 项目,并且您不会在不在 Android 设备上运行的项目中从 Android 导入任何内容。
关于java - 线程 "main"java.lang.RuntimeException : Stub 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9213533/