0
public class Join extends Activity {

EditText id = (EditText)findViewById(R.id.id);
EditText password = (EditText)findViewById(R.id.password);
EditText name = (EditText)findViewById(R.id.name);
EditText phone =(EditText)findViewById(R.id.phone);
Button join = (Button)findViewById(R.id.join);



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if(android.os.Build.VERSION.SDK_INT>9){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    join.setOnClickListener(new OnClickListener() {   

        @Override
        public void onClick(View arg0) {

            String get_id = id.getText().toString();
            String get_password = password.getText().toString();
            String get_name = name.getText().toString();
            String get_phone = phone.getText().toString();



            // I want put in here HttpPostAsyncTask.


        }
    });
}




class HttpPostAsyncTask extends AsyncTask<String,Integer,Long>{
    @Override 
    protected Long doInBackground(String... params){
        String id = params[0];
        String password = params[1];
        String name = params[2];
        String phone = params[3];

        try{
            HttpClient client = new DefaultHttpClient();
            String postUrl = "http://vv9863.dothome.co.kr/member.php";
            HttpPost post = new HttpPost(postUrl);
            List params2 = new ArrayList();
            params2.add(new BasicNameValuePair("id",id));
            params2.add(new BasicNameValuePair("password",password));
            params2.add(new BasicNameValuePair("name",name));
            params2.add(new BasicNameValuePair("phone",phone));

            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params2,HTTP.UTF_8);
            post.setEntity(ent);
            HttpResponse responsePost = client.execute(post);
            HttpEntity resEntity = responsePost.getEntity();

            if(resEntity!=null){
                Log.w("Response",EntityUtils.toString(resEntity));
            }       
        } catch(MalformedURLException e){

        } catch(IOException e){

        }
        return null;


    }
}

}

你好。我是android开发的初学者。

在这段代码中,我想在两个条件中使用“HttpAsyncTask”:

  1. 有参数“get_id”、“get_password”、“get_name”、“get_phone”
  2. 按下按钮“加入”时运行

.. 我应该怎么办..?

4

2 回答 2

0

将以下行添加到您的 onClickListener:

new HttpPostAsyncTask().execute(get_id, get_password, get_name, get_phone);

有关详细信息,请参阅http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-07-21T04:08:01.030 回答
0

只是给你的建议

  • 不要将 ui 元素初始化为 u 初始化(在 Oncreate(..) 方法的外部)在 setContentView(R.layout.main)调用您之后进行初始化,否则这将给出nullPointer Exception

   public class Join extends Activity {

     EditText id;
     EditText password ;
     EditText name ;
     EditText phone ;
     Button join;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        id = (EditText)findViewById(R.id.id);
        password = (EditText)findViewById(R.id.password);
        name = (EditText)findViewById(R.id.name);
        phone =(EditText)findViewById(R.id.phone);
        join = (Button)findViewById(R.id.join);
    }
于 2013-07-21T04:18:31.723 回答