2012-04-04 89 views
0

我從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" 

} 

回答

1

你不能在void方法返回任何東西。如果您希望返回String,則需要將類型爲ContactsandIm()的返回類型添加到String。另外,調用return x將退出該方法,因此返回後的任何代碼都是死代碼。

+0

謝謝,我試過它沒有返回數據,它工作正常。我現在在列表視圖中有「Returnnull」,我的PHP被設置爲返回「True」,所以,它可以工作嗎?但是,爲什麼我在列表視圖的末尾有一個空值。 Image:http://i.imgur.com/i8lsw.png – TheBlueCat 2012-04-04 23:44:51

+0

你爲什麼要追加'System.clearProperty(「line.seperator」)'?如果你擺脫了這一點,那麼「null」應該停止附加到你的回覆。 – 2012-04-04 23:50:16

+0

謝謝!你解決了它!我會選擇你的答案。謝謝,再一次:)我非常生氣。 – TheBlueCat 2012-04-05 00:03:02

1

在你的功能ContactsAndIm之後data = sb.toString();你正在做return data;擺脫那,你應該沒問題。這不是日食,它是java編譯器告訴你,你不能返回一個具有void簽名的函數中的值

+0

我現在有另一個問題。謝謝,我嘗試了它沒有返回數據,它工作正常。我現在在列表視圖中有「Returnnull」,我的PHP被設置爲返回「True」,所以,它可以工作嗎?但是,爲什麼我在列表視圖的末尾有一個空值。圖片:i.imgur.com/i8lsw.png – TheBlueCat 2012-04-04 23:46:32