Java 为什么我会收到“不兼容的类型:对象无法转换为字符串”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22791834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:53:30  来源:igfitidea点击:

Why am I getting, "incompatible types: Object cannot be converted to String" with this?

javaandroidandroid-studiohttp-getandroid-gradle-plugin

提问by B. Clay Shannon

I'm trying to use the simplest possible code for calling a Web API REST method from an Android app, and the code that I found herelooked very promising:

我正在尝试使用最简单的代码从 Android 应用程序调用 Web API REST 方法,我在这里找到的代码看起来非常有前途:

public String callWebService(String requestUrl)
{
    String deviceId = "Android Device";

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(requestUrl);
    request.addHeader("deviceId", deviceId);

    ResponseHandler handler    = new BasicResponseHandler();
    String result = "";

    try
    {
        result = httpclient.execute(request, handler); // <= a line too far
    }
    catch (ClientProtocolException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

    httpclient.getConnectionManager().shutdown();

    return result;
}

However, it won't compile, telling me: "incompatible types: Object cannot be converted to String" on this line:

但是,它不会编译,在这一行告诉我:“不兼容的类型:对象无法转换为字符串”:

result = httpclient.execute(request, handler);

It does give a couple of options in an attempt to circumvent the logjam:

它确实提供了几个选项来试图绕过僵局:

enter image description here

在此处输入图片说明

...but I don't know which, if any, of these options is the preferred way to solve this dilemma. Is one way "the way"?

...但我不知道这些选项中的哪一个(如果有的话)是解决这个困境的首选方法。一种方式是“方式”吗?

UPDATE

更新

This code, as I said, looks promising to me, however I think it's basically unusable as is, because it gives me the dreaded "NetworkOnMainThreadException" From logcat:

正如我所说,这段代码对我来说看起来很有希望,但是我认为它基本上无法使用,因为它给了我可怕的“ NetworkOnMainThreadException”来自 logcat:

04-01 13:18:41.861    1267-1267/hhs.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
. . .
    java.lang.IllegalStateException: Could not execute method of the activity
. . .
     Caused by: java.lang.reflect.InvocationTargetException
. . .
     Caused by: android.os.NetworkOnMainThreadException

采纳答案by Sotirios Delimanolis

Because you are using a raw type in

因为您使用的是原始类型

ResponseHandler handler = ...

With raw types, the type variables in the method declarations are erased. So everything appears as Object(or whatever the leftmost bound of the type parameter is).

对于原始类型,方法声明中的类型变量将被删除。所以一切都显示为Object(或任何类型参数最左边的边界)。

Instead, use a parameterized type

相反,使用参数化类型

ResponseHandler<String> handler = ...

This also works because BasicResponseHandlerextends ResponseHandler<String>.

这也有效,因为BasicResponseHandlerextends ResponseHandler<String>

Now

现在

httpclient.execute(request, handler);

will have a return type associated with the type argument used when declaring handler, which is Stringand the result can therefore be assigned to a Stringvariable (or anywhere a Stringis expected).

将具有与声明时使用的类型参数关联的返回类型handlerString因此可以将结果分配给String变量(或String预期的任何地方)。

回答by Ivan Verges

Try this:

尝试这个:

result = httpclient.execute(request, handler).toString();

If i'm not wrong, you should be able to use the "toString" method to convert the return of execute method to String type.

如果我没猜错,您应该可以使用“toString”方法将 execute 方法的返回值转换为 String 类型。