java - 无法返回数据

标签 java android json listview append

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

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

代码:

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"

}

最佳答案

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

关于java - 无法返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10020773/

相关文章:

java - Eclipse启动报错java.lang.ClassNotFoundException

android - AWS S3 Java SDK : detect time/clock skew programmatically?

java - 一步一步...使用 NFC 读取标签

android - 通过蓝牙将安卓麦克风连接到手机

java - 如何反序列化 jackson 中两个列表的数组

java - 哪个集合最适合存储键值对,但我必须同时搜索键和值

java - 如何构建 .jar 文件

java - 将 BiPredicate 作为函数参数传递

jquery - 从 Google 电子表格获取 JSON 数据 - 未捕获的类型错误

jquery - 解析数组中的JSON数组并使用键值