【问题标题】:Can't return Data无法返回数据
【发布时间】:2012-04-18 17:32:30
【问题描述】:

我从 HttpGet 返回数据,然后使用适配器将字符串传递到 Listview。问题是,在我将数据返回到字符串后,Eclipse 不允许我返回数据。

错误:无效方法无法返回值。

代码:

public class ChatService extends ListActivity {
    /** Called when the activity is first created. */

    BufferedReader in = null;
    String data = null;
    List headlines;
    List links;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        try {
            ContactsandIm();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        CheckLogin();
    }





    private void CheckLogin() {
        // TODO Auto-generated method stub
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();

        /* login.php returns true if username and password is equal to saranga */
        HttpPost httppost = new HttpPost("http://gta5news.com/login.php");

        try {

            // Execute HTTP Post Request
            Log.w("HttpPost", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);

            String str = inputStreamToString(response.getEntity().getContent())
                    .toString();
            Log.w("HttpPost", str);

            if (str.toString().equalsIgnoreCase("true")) {
                Log.w("HttpPost", "TRUE");
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // put intent here(21/3/12);

            } else {
                Log.w("HttpPost", "FALSE");

            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }

    public void ContactsandIm() throws URISyntaxException,
            ClientProtocolException, IOException {
        headlines = new ArrayList();

        // TODO Auto-generated method stub
        BufferedReader in = null;
        String data = null;

        HttpClient get = new DefaultHttpClient();
        URI website = new URI("http://www.gta5news.com/test.php");
        HttpGet webget = new HttpGet();
        webget.setURI(website);
        HttpResponse response = get.execute(webget);
        Log.w("HttpPost", "Execute HTTP Post Request");
        in = new BufferedReader(new InputStreamReader(response.getEntity()
                .getContent()));
        StringBuffer sb = new StringBuffer("");
        String l ="";
        String nl = System.clearProperty("line.seperator");
        while ((l =in.readLine()) !=null) {
            sb.append(l + nl);  
        }
        in.close();
         data = sb.toString();
        return data;

        headlines.add(sb);


        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, headlines);

        setListAdapter(adapter);



    }

    // end bracket for "ContactsandIm"

}

【问题讨论】:

    标签: java android json listview append


    【解决方案1】:

    data = sb.toString(); 之后的函数 ContactsAndIm 中,你正在做 return data; 摆脱它,你应该没问题。不是eclipse,是java编译器告诉你不能在带有void签名的函数中返回值

    【讨论】:

    • 我现在有另一个问题。谢谢,我在没有返回数据的情况下尝试了它,它工作正常。我现在在列表视图中有“Returnnull”,我的 PHP 设置为它返回“True”,所以它可以工作吗?但是,为什么我在列表视图的末尾有一个空值。图片:i.imgur.com/i8lsw.png
    【解决方案2】:

    您不能在 void 方法中返回任何内容。如果您希望返回String,则需要将返回类型添加到String 类型的ContactsandIm()。另外,调用return x会退出方法,所以返回后的代码都是死代码。

    【讨论】:

    • 谢谢,我在没有返回数据的情况下试过了,效果很好。我现在在列表视图中有“Returnnull”,我的 PHP 设置为它返回“True”,所以它可以工作吗?但是,为什么我在列表视图的末尾有一个空值。图片:i.imgur.com/i8lsw.png
    • 为什么要附加System.clearProperty("line.seperator")?如果您摆脱它,“null”应该停止附加到您的响应中。
    • 谢谢!你解决了!我会选择你的答案。谢谢,再次感谢:) 我太夸张了。
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 1970-01-01
    • 2020-04-17
    • 2021-06-24
    • 1970-01-01
    相关资源
    最近更新 更多