类型不匹配:不能转换的StringBuilder为String [英] Type mismatch: cannot convert from StringBuilder to String

查看:1543
本文介绍了类型不匹配:不能转换的StringBuilder为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个方法返回给定的URL的来源。

 私有静态字符串getUrlSource(字符串URL){
    尝试{
        URL localUrl = NULL;
        localUrl =新的URL(网址);
        康涅狄格州的URLConnection = localUrl.openConnection();
        读者的BufferedReader =新的BufferedReader(
            新的InputStreamReader(conn.getInputStream()));
        串线=;
        字符串HTML;
        StringBuilder的MA =新的StringBuilder();
        而((行= reader.readLine())!= NULL){
            ma.append(线);
        }
        返回马;
    }赶上(例外五){
        Log.e(ERR,e.getMessage());
    }
}

它给了我这个错误:

 类型不匹配:不能转换的StringBuilder为String

和两种选择:


  1. 更改返回类型的StringBuilder。
    但我想它返回一个字符串。

  2. 马为String的更改类型。
    改变后的字符串没有append()方法。


解决方案

只需使用

 返回ma.toString();

而不是

 回马;

ma.toString()返回字符串重新presentation为您的StringBuilder。

请参阅StringBuilder#toString()详情

由于瓦列Atamaniouk的意见建议,你应该也返回了块的东西,否则你将得到一个编译器错误缺少return语句,所以编辑

 }赶上(例外五){
    Log.e(ERR,e.getMessage());
}

 }赶上(例外五){
    Log.e(ERR,e.getMessage());
    返回null; //或者返回另一个字符串
}

将是一个好主意。


修改

由于Esailija建议,我们在这个code三防模式

 }赶上(例外五){//你应该抓住特定异常
    Log.e(ERR,e.getMessage()); //不要记录异常,扔掉它,让来电处理它
    返回null; //不要返回null,如果它是不必要的
}

所以我认为这是更好地做这样的事情:

 私有静态字符串getUrlSource(字符串URL)抛出MalformedURLException的,IOException异常{
    URL localUrl = NULL;
    localUrl =新的URL(网址);
    康涅狄格州的URLConnection = localUrl.openConnection();
    读者的BufferedReader =新的BufferedReader(
            新的InputStreamReader(conn.getInputStream()));
    串线=;
    字符串HTML;
    StringBuilder的MA =新的StringBuilder();
    而((行= reader.readLine())!= NULL){
        ma.append(线);
    }
    返回ma.toString();
}

然后,当你调用它:

  {尝试
    串urlSource = getUrlSource(http://www.google.com);
    //处理您的网址源
}赶上(MalformedURLException的前){
    //您的网址是错误的,在这里做一些东西
}赶上(IOException异常前){
    // I / O操作被打断,在这里做一些东西
}

检查这些链接,关于Java反模式的详细信息:

This method returns the source of the given URL.

private static String getUrlSource(String url) {
    try {
        URL localUrl = null;
        localUrl = new URL(url);
        URLConnection conn = localUrl.openConnection();
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
        String line = "";
        String html;
        StringBuilder ma = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            ma.append(line);
        }
        return ma;
    } catch (Exception e) {
        Log.e("ERR",e.getMessage());
    }
}

It gives me this error:

Type mismatch: cannot convert from StringBuilder to String

And two choices:

  1. Change the return type to StringBuilder. But I want it to return a String.
  2. Change type of ma to String. After changing a String has no append() method.

解决方案

Just use

return ma.toString();

instead of

return ma;

ma.toString() returns the string representation for your StringBuilder.

See StringBuilder#toString() for details

As Valeri Atamaniouk suggested in comments, you should also return something in the catch block, otherwise you will get a compiler error for missing return statement, so editing

} catch (Exception e) {
    Log.e("ERR",e.getMessage());
}

to

} catch (Exception e) {
    Log.e("ERR",e.getMessage());
    return null; //or maybe return another string
}

Would be a good idea.


EDIT

As Esailija suggested, we have three anti-patterns in this code

} catch (Exception e) {           //You should catch the specific exception
    Log.e("ERR",e.getMessage());  //Don't log the exception, throw it and let the caller handle it
    return null;                  //Don't return null if it is unnecessary
}

So i think it is better to do something like that:

private static String getUrlSource(String url) throws MalformedURLException, IOException {
    URL localUrl = null;
    localUrl = new URL(url);
    URLConnection conn = localUrl.openConnection();
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String line = "";
    String html;
    StringBuilder ma = new StringBuilder();
    while ((line = reader.readLine()) != null) {
        ma.append(line);
    }
    return ma.toString();
}

And then, when you call it:

try {
    String urlSource = getUrlSource("http://www.google.com");
    //process your url source
} catch (MalformedURLException ex) {
    //your url is wrong, do some stuff here
} catch (IOException ex) {
    //I/O operations were interrupted, do some stuff here
}

Check these links for further details about Java Anti-Patterns:

这篇关于类型不匹配:不能转换的StringBuilder为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆