【问题标题】:Exception in thread "main" java.lang.RuntimeException: Stub线程“主”java.lang.RuntimeException 中的异常:存根
【发布时间】:2012-02-09 15:17:39
【问题描述】:

大家好,我遇到了这个奇怪的错误,我不知道为什么?

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)`

【问题讨论】:

标签: java android apache exception httpclient


【解决方案1】:

您在此处导入了 android SDK。您是否有机会尝试从您的电脑上运行它(例如,从标准的 java 项目)?

此错误意味着您无法访问您尝试使用的实际方法或对象,而只能访问存根 - 它所做的只是抛出您看到的这个异常。

确保您在模拟器(或 Android 设备)上运行您的 android 项目,并且您不会在未在 android 设备上运行的项目中从 android 导入任何内容。

【讨论】:

    【解决方案2】:

    您之所以会这样,是因为 Android 包中的 apache 类与 JVM 中的不同。向常规 apache 类添加依赖项,如果您使用的是 maven,请确保在测试平台中的 android 平台之前加载这些依赖项(与 jUnit 4 需要做的相同)。之后,您将能够在 JVM 中测试包含 apache HTTP 方法的类,但要考虑到 JVM 不会对服务器进行任何调用,因此这仅对测试您的所有其他部分有用类。

    【讨论】:

      猜你喜欢
      • 2015-03-21
      • 1970-01-01
      • 2017-01-06
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      相关资源
      最近更新 更多