2

我有一个登录页面,我想从网络上的数据库中验证用户名和密码。问题是在执行 AsyncTask 时我想返回用户名和密码值。但这并没有发生。

如何返回值?这是我的登录页面代码。

public class Login extends Activity {
Integer aaa=0;
Button b,b2;
RelativeLayout r;
TextView t, t2;
String str1, str2, username, password;
String A = null, B = null;
EditText et1, et2;
Dialog myDialog;
String FILENAME = "http://animsinc.com/query.php";
protected ProgressDialog dialog;
protected Handler h;
static InputStream in = null;
static JSONObject jObj = null;
static String json = "";
private static final String TAG_ID = "id";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_in);

    Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE))
            .getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight() / 4;
    r = (RelativeLayout) findViewById(R.id.RL);
    RelativeLayout.LayoutParams r = new RelativeLayout.LayoutParams(width,
            height);
    t = (TextView) findViewById(R.id.textView1);
    t2 = (TextView) findViewById(R.id.textView2);
    t.setOnClickListener(link);
    t2.setOnClickListener(fgtpass);

    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);

    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(temp);

    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if ((et1.getText().length() == 0)
                    || (et2.getText().length() == 0))

            {

                Toast.makeText(getApplicationContext(),
                "Please enter correct details",Toast.LENGTH_LONG)
                        .show();

            } else {

                dialog = ProgressDialog.show(Login.this, "Loading",
                        "Please Wait...");

            /*  h = new Handler() {

                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        dialog.dismiss();
                    }
                };

                new Thread() {

                    @Override
                    public void run() {
                        super.run();

                        String st=startDownload();

                        try {
                            Thread.sleep(3000);
                            h.sendEmptyMessage(0);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }

                }.start();*/

     Toast.makeText(getApplicationContext(),""+st,Toast.LENGTH_LONG).show();
        String st=startDownload();
 Toast.makeText(getApplicationContext(),"aaa="+aaa, Toast.LENGTH_LONG).show();
 Toast.makeText(getApplicationContext(),""+st, Toast.LENGTH_LONG).show();
            }
if ((et1.getText().toString().equals(username))&&           (et2.getText().toString().equals(password)))
           {
    Intent openStartingPoint = new Intent(Login.this,
                        UserActivity.class);
                startActivity(openStartingPoint);
            }

        }

    });


}

private 
String startDownload() {
    String C = null;
    new AppTask().execute(FILENAME);
    aaa++;
    return C;
}


private View.OnClickListener temp = new View.OnClickListener() {
    public void onClick(View V) {
        Intent openStartingPoint = new Intent(Login.this,
                UserActivity.class);
        startActivity(openStartingPoint);
    }
};

private View.OnClickListener link = new View.OnClickListener() {
    public void onClick(View V) {
        Intent openStartingPoint = new Intent(Login.this,
                ContactDetails.class);
        startActivity(openStartingPoint);
    }
};

private View.OnClickListener fgtpass = new View.OnClickListener() {
    public void onClick(View V) {
        myDialog = new Dialog(Login.this);
        myDialog.setContentView(R.layout.emailpop);
        myDialog.setTitle("Forgot Password");
        myDialog.setCancelable(true);

        // for save
        Button ok = (Button) myDialog.findViewById(R.id.button1);
        ok.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                myDialog.dismiss();

            }
        });
        myDialog.show();
    }
};


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

}

public class AppTask extends AsyncTask<String, Integer, String> {

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

    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... params) {
        String is = null;
        str1 = et1.getText().toString();
        str2 = et2.getText().toString();

        if (str1.length() > 0 && str2.length() > 0) {
            A = str1;
            B = str2;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://animsinc.com/query.php");
            try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
        nameValuePairs.add(new BasicNameValuePair("username", str1));
        nameValuePairs.add(new BasicNameValuePair("password", str2));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);

        HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = EntityUtils.toString(entity);

                JSONArray jArray = new JSONArray(is);

                for (int i = 0; i < jArray.length(); i++) {

            JSONObject jObject = jArray.getJSONObject(i);

                    username = jObject.getString("username");
                    password = jObject.getString("password");
                    aaa++;
                }

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } 

        return username;
    }

}

}
4

2 回答 2

2

当一个异步任务被执行时,任务会经过 4 个步骤:

  1. onPreExecute(),在任务执行之前在 UI 线程上调用。此步骤通常用于设置任务,例如通过在用户界面中显示进度条。

  2. doInBackground(Params...),在 onPreExecute() 完成执行后立即在后台线程上调用。此步骤用于执行可能需要很长时间的后台计算。异步任务的参数传递到这一步。计算的结果必须由这一步返回,并将传递回最后一步。此步骤还可以使用 publishProgress(Progress...) 来发布一个或多个进度单位。这些值在 UI 线程上的 onProgressUpdate(Progress...) 步骤中发布。

  3. onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行的时间是不确定的。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可用于动画进度条或在文本字段中显示日志。

  4. onPostExecute(Result),在后台计算完成后在 UI 线程上调用。后台计算的结果作为参数传递给这一步

所以在 doInBackground() 中返回值,在 onPostExecute() 中接收它并相应地更新 ui。

  @Override
  protected void onPostExecute(String result) {
  super.onPostExecute(result);
  Toast.makeTest(MainActivity.this,result,1000).show();  
  //set result to textview   

 }
于 2013-03-28T05:47:06.250 回答
0
@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

// result is the value you return from doInBackground     
String username = result;
}
于 2013-03-28T05:41:08.367 回答