Reputation: 1591
Hey everyone I am getting this strange error and I can't figure out why?
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();
}
}
}
}
}
and the error is this
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)`
Upvotes: 6
Views: 14946
Reputation: 2701
You are getting that because the apache classes in the Android package are different from the ones in the JVM. Add a dependency to the regular apache classes, and, if you are using maven, make sure those dependencies are loaded before the android platform in your test platform (kinda the same you need to do with jUnit 4). After that you will be able to test your classes that contains apache HTTP methods in the JVM, but take in consideration that the JVM is not going to do any calls to the server, so this is useful only to test all the other parts of your class.
Upvotes: 0
Reputation:
You have android SDK imports here. Are you by any chance trying to run this from your pc (like, from a standard java project)?
This error means you don't have access to the actual method or object you are trying to use, but only to a stub - a method that all it does is throw this exception you see.
Make sure you run your android projects on the emulator (or on an android device) and that you don't import anything from android in a project that does not run on an android device.
Upvotes: 10