gpt4 book ai didi

java - onPostExecute 从服务器检索 null

转载 作者:行者123 更新时间:2023-12-01 18:12:33 25 4
gpt4 key购买 nike

我尝试使用 AsyncTask 从服务器读取数据,但是当我将参数提供给 onPostExecute 时,它​​检索到 null。MainActivity 类:

public class MainActivity extends Activity{

EditText name, password;
Button login;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);


login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView uiUpdate = (TextView) findViewById(R.id.output);
String outputasync = uiUpdate.getText().toString();
String serverURL = "http://192.168.1.105/myapp/text.php";
LongOperation longOperation = new LongOperation(MainActivity.this);
longOperation.execute(serverURL);
longOperation.onPostExecute(uiUpdate);
}
});



}

异步任务:

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


private Context mcontext;
private String content;
private String error = null;
AlertDialog alertDialog;


public LongOperation(Context context){
mcontext = context ;
}

@Override
protected void onPreExecute() {

alertDialog = new AlertDialog.Builder(mcontext).create();
alertDialog.setTitle("Login Information....");
}

@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection client = (HttpURLConnection)url.openConnection();
client.connect();
InputStream inputStream = client.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
content = bufferedReader.readLine();

bufferedReader.close();
inputStream.close();
client.disconnect();

} catch (IOException e) {
error = e.getMessage();
}

return null;
}

protected void onPostExecute(TextView unused) {

alertDialog.dismiss();

if (error != null) {

unused.setText("Output : " + error);

} else {

unused.setText("Output : "+ content);

}

}


}

与服务器的连接正确,问题是在 TextView 中显示服务器内的消息。

更新和解决方案

就像男性狂说的:

You should not be calling onPostExecute manually from your code. Calling execute on the asynctask should suffice. onPostExecute will automatically be called when the asynctask finishes its work.

并将onPostExecute参数更改为String

为了检索带有服务器消息的 TextView,我做了 Sharj 所说的:

2) How to set your TextView that is in your Activity. The simplest way is to pass activity variable to LongOperation constructor and use that for accessing TextView in onPostExecute.

异步任务:

public class LongOperation  extends AsyncTask<String, Void, String> {
TextView textviews;
private Context mcontext;
private String content;
private String error = null;
AlertDialog alertDialog;


public LongOperation(Context context, TextView textView){
textviews = textView;
mcontext = context ;


}

@Override
protected void onPreExecute() {

alertDialog = new AlertDialog.Builder(mcontext).create();
alertDialog.setTitle("Login Information....");
}

@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection client = (HttpURLConnection)url.openConnection();
client.connect();
InputStream inputStream = client.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
content = bufferedReader.readLine();

bufferedReader.close();
inputStream.close();
client.disconnect();

} catch (IOException e) {
error = e.getMessage();
}

return null;
}
@Override
protected void onPostExecute(String unused) {

alertDialog.dismiss();

if (error != null) {

unused=("Output : " + error);
textviews.setText(unused);

} else {

unused=("Output : "+ content);
textviews.setText(unused);


}

}

MainActivity 类:

public class MainActivity extends Activity{

EditText name, password;
Button login;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);


login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView uiUpdate = (TextView) findViewById(R.id.output);
String outputasync = uiUpdate.getText().toString();
String serverURL = "http://192.168.1.105/myapp/text.php";
LongOperation longOperation = new LongOperation(MainActivity.this, uiUpdate);
longOperation.execute(serverURL, outputasync);

}
});



}

注意:doInBackground 仍然使用“return = null”,因为我只是用它来读取服务器内部的数据,而不是在任何地方检索它。

最佳答案

您不应该从代码中手动调用onPostExecute。在 asynctask 上调用 execute 就足够了。当 asynctask 完成其工作时,onPostExecute 将自动调用。

关于java - onPostExecute 从服务器检索 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31887340/

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