将字符串传递给Asynctask

时间:2014-03-02 07:57:16

标签: android android-asynctask

我正在尝试将输入的密码值传递给对话框并将其传递给asyntask,以便将其发送到数据库进行比较。我在密码字段中获得nullpointerexception。似乎价值没有通过。我该如何解决这个问题?

需要密码才能继续的对话框:

public void onClick(View view) {
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            View promptView = layoutInflater.inflate(
                    R.layout.prompt_password, null);
            AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setView(promptView);

            // Set an EditText view to get user input
            final EditText input = (EditText) promptView
                    .findViewById(R.id.passwordInput);

            alert.setPositiveButton("OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            String pass = (input.getText()).toString();
                            // Do something with value!
                            Log.d("Value", pass);
                            ProgressDialog progressDialog = new ProgressDialog(DisplayReqItemInfo.this);
                            progressDialog.setMessage("Cheking password...");

                            ItemEdit itemEdit = new ItemEdit(DisplayReqItemInfo.this, progressDialog);
                            itemEdit.execute();
                        }
                    });

            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // Canceled.
                            dialog.cancel();
                        }
                    });

            AlertDialog dialog = alert.create();
            dialog.show();

        }
    });

}

假设要检索值的类:

public class ItemEdit extends AsyncTask<String, Void, Integer> {

private ProgressDialog progressDialog;
private DisplayReqItemInfo activity;

private int responseCode = 0;

public ItemEdit(DisplayReqItemInfo activity, ProgressDialog progressDialog)
{
    this.activity = activity;
    this.progressDialog = progressDialog;
}

@Override
protected void onPreExecute()
{
    progressDialog.show();
}

protected Integer doInBackground(String... arg0) {
    TextView PID = (TextView)activity.findViewById(R.id.req_pid);
    EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput);
    String pid = PID.getText().toString();  
    String password = passwordEdit.getText().toString();
    ItemFunction itemFunction = new ItemFunction();
    JSONObject json = itemFunction.requestItem(pid, password);

    // check for response
    try {
        if (json.getString(KEY_SUCCESS) != null) {
            String res = json.getString(KEY_SUCCESS);

            if(Integer.parseInt(res) == 1){

                responseCode = 1;

            }else{
                responseCode = 0;
            }
        }

    } catch (NullPointerException e) {
        e.printStackTrace();

    }
    catch (JSONException e) {
        e.printStackTrace();
    }

    return responseCode;
}

@Override
protected void onPostExecute(Integer responseCode)
{   
    if (responseCode == 1) {
        progressDialog.dismiss();
        Intent i = new Intent();
        i.setClass(activity.getApplicationContext(), MainMenu.class);
        activity.startActivity(i);
    }
    else {
        progressDialog.dismiss();

    }

}
}

2 个答案:

答案 0 :(得分:1)

您可以在execute

中传递参数
  ItemEdit itemEdit = new ItemEdit(DisplayReqItemInfo.this, progressDialog);
  itemEdit.execute(new String[] { "yourstring" });

Async doInBackground

protected Integer doInBackground(String... arg0) {
 String response = args0[0];
TextView PID = (TextView)activity.findViewById(R.id.req_pid);

答案 1 :(得分:0)

AsyncTask.doInBackground函数内部不能有任何屏幕I / O.您可以在onPreExecute或onPostExecute函数中执行屏幕I / O,这些函数可以访问UI线程。因此,您可以从onPreExecute中的EditText字段获取值,并将其放入一个对AsyncTask方法是全局的变量,并由doInBackground读取。

(使用你的例子)

public class ItemEdit extends AsyncTask<String, Void, Integer> {
String password;
String pid;

  @Override
  protected void onPreExecute()
  {
    TextView PID = (TextView)activity.findViewById(R.id.req_pid);
    EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput);

    pid = PID.getText().toString();  
    password = passwordEdit.getText().toString();
  }

  protected Integer doInBackground(String... arg0) {
    ItemFunction itemFunction = new ItemFunction();
    JSONObject json = itemFunction.requestItem(pid, password);

    . . . 
  }
}

您还可以将参数传递到AsyncTask初始值设定项并通过类global传递:

TextView PID = (TextView)activity.findViewById(R.id.req_pid);
EditText passwordEdit = (EditText)activity.findViewById(R.id.passwordInput);

String pid = PID.getText().toString();  
String password = passwordEdit.getText().toString();

// start background task
mAuthTask = new UserLoginTask(pid, password);
mAuthTask.execute();


public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
  private String userLoginId;
  private String userPassword;

  private UserLoginTask(String loginId, String password) {
    userLoginId = loginId;
    userPassword = password;
  }

  @Override
  protected Boolean doInBackground(Void... params) {

        . . .

        // Do something with userLoginId, userPassword 

        . . . 
  }
}