大家好,我收到了这个奇怪的错误,我不知道为什么?
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项目中)?
这个错误意味着你不能访问你试图使用的实际方法或对象,而只能访问一个存根——这个方法所做的就是抛出你看到的这个异常。
确保你在模拟器上(或在安卓设备上)运行你的安卓项目,并且你不会在不在安卓系统设备上运行的项目中从安卓系统导入任何内容。
您之所以得到这一点,是因为Android包中的apache类与JVM中的类不同。向常规的apache类添加一个依赖项,如果您使用maven,请确保这些依赖项在测试平台中的android平台之前加载(与jUnit4需要做的有点相同)。之后,您将能够在JVM中测试包含apacheHTTP方法的类,但要考虑到JVM不会对服务器进行任何调用,因此这只适用于测试类的所有其他部分。