gpt4 book ai didi

java - 无法返回数据

转载 作者:行者123 更新时间:2023-12-02 07:52:48 25 4
gpt4 key购买 nike

我从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com