Can't return Data

Go To StackoverFlow.com

0

I'm returning data from a HttpGet and then passing the string(s) into a Listview with an Adapter. The thing is, Eclipse isn't letting me return the data after I've returned it to a string.

Error: Void methods cannot return a value.

Code:

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"

}
2012-04-04 23:38
by TheBlueCat


1

You can't return anything in a void method. You need to add a return type to ContactsandIm() of type String if you wish to return a String. Also, calling return x will exit the method, so any code after the return is dead code.

2012-04-04 23:41
by Tyler Treat
Thanks, I tried it without the return data and it worked fine. I now have "Returnnull" in the listview, My PHP is set up that it returns "True" so, could this be it working? But, why do I have a null at the end of the listview. Image: http://i.imgur.com/i8lsw.pn - TheBlueCat 2012-04-04 23:44
Why are you appending System.clearProperty("line.seperator")? If you get rid of that, the "null" should stop getting appended to your response - Tyler Treat 2012-04-04 23:50
Thanks! You solved it! I'll select your answer. Thanks, once again:) I was bloviating, so much - TheBlueCat 2012-04-05 00:03


1

In your function ContactsAndIm right after data = sb.toString(); you are doing return data; get rid of that and you should be ok. It's not eclipse, it's the java compiler telling you that you can't return a value in a function with a void signature

2012-04-04 23:42
by Matt Phillips
I now have another problem. Thanks, I tried it without the return data and it worked fine. I now have "Returnnull" in the listview, My PHP is set up that it returns "True" so, could this be it working? But, why do I have a null at the end of the listview. Image: i.imgur.com/i8lsw.pn - TheBlueCat 2012-04-04 23:46
Ads