gpt4 book ai didi

java - 使用 doInBackground 的结果设置 textView

转载 作者:行者123 更新时间:2023-12-02 13:22:06 26 4
gpt4 key购买 nike

我是 Android 新手,我尝试使用 php 脚本与本地主机进行通信并访问一个简单的数据库。我在 doInBackground() 方法中定义了一个任务,该任务从存储在本地主机上的数据库中获取值(我不知道该部分是否有效)。我想使用 doInBackground 方法返回的结果在 Activity 的 TextView 中设置文本。

public class BackgroundWorker extends AsyncTask<String,Void,String> {

Context context;


BackgroundWorker(Context ctx)
{
context = ctx;
}

@Override
protected String doInBackground(String... params) {
String group = params[0];
String child = params[1];
String address = "http://10.0.2.2/conn.php";

URL url = null;
try {
url = new URL(address);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("group", "UTF-8") + "=" + URLEncoder.encode(group, "UTF-8") + "&"
+ URLEncoder.encode("child", "UTF-8") + "=" + URLEncoder.encode(child, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected void onPostExecute(String s) {

super.onPostExecute(s);
}

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

以及 Activity 类:

public class viewTT extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_tt);


Button btnNextScreen = (Button) findViewById(R.id.button);
TextView txtName = (TextView) findViewById(R.id.textView);
TextView txtName2 = (TextView) findViewById(R.id.textView2);

Intent i = getIntent();
// Receiving the Data
String group= i.getStringExtra("group");
String child = i.getStringExtra("child");
txtName.setText(group+" "+child);

BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(group,child);



btnNextScreen.setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0)
{
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), MainActivity.class);
startActivity(nextScreen);
}
});
}
}

我想设置txtName2。

最佳答案

您可以使用接口(interface)将数据返回到您的 Activity

界面

    public interface AsyncResponse {
public void onFinish(Object output);
}

SomeAsyncTask 类

public class SomeAsyncTask extends AsyncTask<String, String, String> {
private AsyncResponse asyncResponse;

public SomeAsyncTask(AsyncResponse asyncResponse) {
this.asyncResponse = asyncResponse;

}

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

//Do something
.....
//Finally return something

return "returnSomeString";
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
asyncResponse.onFinish(s);
}}

在要设置 View 的 Activity 中,像这样调用 SomeAsyncTask 类

    SomeAsyncTask someAsyncTask=new SomeAsyncTask(new AsyncResponse() {
@Override
public void onFinish(Object output) {
String result= (String) output;
//Finally set your views
}
});
someAsyncTask.execute();
}

关于java - 使用 doInBackground 的结果设置 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43520792/

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