java - onPostExecute 从服务器检索 null

标签 java android android-asynctask

我尝试使用 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/

相关文章:

Java:如何实现私有(private)抽象方法?

java - 更改 Android 中的字体会导致应用程序崩溃吗?

java - Android - 将(字符串...路径)从 asynctask 更改为位于服务中的函数

java - Android 的 WSDL 到 java 的转换

android - 如何实现这些水平滚动的卡片设计

android - 使用 AsyncTask 下载 XML 文件

java - 安卓 : How to Pass the JSON object From AsncTask class to the Main Class

java - 如何为在 jboss 上运行并使用 ojdbc14.jar 的闭源应用程序启用 SQL 跟踪

java - android从数据库获取后返回null无法启动 Activity ComponentInfo : java. lang.NullPointerException:存储== null

java - apache.commons.exec - 吞下执行进程抛出的异常?