gpt4 book ai didi

java - 将字符串从异步任务的 doInBackground 传递到主线程

转载 作者:太空宇宙 更新时间:2023-11-04 15:15:00 24 4
gpt4 key购买 nike

这里是 Java 半菜鸟。

我正在尝试在异步任务的 doInBackground() 内设置 TextView。根据我的研究,我无法修改执行此操作的主线程,因此在这里使用 TextViews 是不可能的。所以我想做的是使用字符串。我需要在主类中可以访问该字符串。

我该怎么做?

我尝试了String loginresult =“登录成功!请稍候...”;但我无法在任何地方访问该字符串。我尝试将其标记为 public 但这是 doInBackground() 内的非法修饰符。

也许字符串不是执行此操作的最佳方法,如果是这样,你们所有的天才会建议什么?

这是我的异步代码,我在遇到问题的区域放置了箭头。任何帮助将不胜感激这个菜鸟:)

class PostTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();



}

@Override
protected String doInBackground(String... params) {


try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
//Executes link, login.php returns true if username and password match in db
HttpResponse response = httpclient.execute(httppost);

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

if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
-----> result.setText("Login Successful! Please Wait...");
}else
{
Log.w("SENCIDE", "FALSE");
------> result.setText(str);
}

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



// Dummy code
for (int i = 0; i <= 100; i += 5) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);
}
return "All Done!";
}//end doinbackground

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;
}//end StringBuilder

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);

}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

// turns the text in the textview "Tbl_result" into a text string called "tblresult"
TextView tblresult = (TextView) findViewById(R.id.tbl_result);




// If "tblresult" text string matches the string "Login Successful! Please Wait..." exactly, it will switch to next activity
if (tblresult.getText().toString().equals("Login Successful! Please Wait...")) {
Intent intent = new Intent(NewAndroidLogin.this, Homepage.class);
//take text in the username/password text boxes and put them into an extra and push to next activity
EditText uname2 = (EditText)findViewById(R.id.txt_username);
String username2 = uname2.getText().toString();
EditText pword2 = (EditText)findViewById(R.id.txt_password);
String password2 = pword2.getText().toString();
intent.putExtra("username2", username2 + "&pword=" + password2);
startActivity(intent);
}

}//end onPostExecute
}//end async task

最佳答案

更改您的AsyncTask以使用String作为进度参数类型:

AsyncTask<String, String, String>

更改onProgressUpdate()以更新进度

@Override
protected void onProgressUpdate(String... values) {
result.setText(values[0]);
}

然后报告进度:

if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
publishProgress("Login Successful! Please Wait...");
}else
{
Log.w("SENCIDE", "FALSE");
publishProgress(str);
}

关于java - 将字符串从异步任务的 doInBackground 传递到主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20943837/

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