首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步任务android

异步任务android
EN

Stack Overflow用户
提问于 2013-03-28 13:33:59
回答 2查看 318关注 0票数 2

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

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

代码语言:javascript
复制
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;
    }

}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-28 13:47:06

执行异步任务时,任务会经历4个步骤:

在执行任务之前在UI线程上调用的

  1. onPreExecute(),。此步骤通常用于设置任务,例如,在onPreExecute()完成执行后立即在后台线程上调用的用户interface.
  2. doInBackground(Params...),中显示进度条。此步骤用于执行可能需要较长时间的后台计算。将异步任务的参数传递到此步骤。计算的结果必须由这一步返回,并将传递回最后一步。此步骤还可以使用publishProgress(Progress...)发布一个或多个进度单位。这些值发布在UI线程的onProgressUpdate中(Progress...)在调用publishProgress之后在UI线程上调用了step.
  3. onProgressUpdate(Progress...),(进度...)。执行的时间未定义。此方法用于在后台计算仍在执行时在用户界面中显示任何形式的进度。例如,它可以用于在后台计算完成后在UI线程上调用的文本field.
  4. onPostExecute(Result),中显示进度条或显示日志。后台计算的结果将作为parameter.

传递到此步骤

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

代码语言:javascript
复制
  @Override
  protected void onPostExecute(String result) {
  super.onPostExecute(result);
  Toast.makeTest(MainActivity.this,result,1000).show();  
  //set result to textview   

 }
票数 2
EN

Stack Overflow用户

发布于 2013-03-28 13:41:08

代码语言:javascript
复制
@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

// result is the value you return from doInBackground     
String username = result;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15674908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档