2013-07-21 215 views
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; 


    } 
} 

}我想運行「HttpAsyncTask」...我該怎麼做?

嗨。我是Android開發人員的初學者。

在這段代碼中,我想在兩種條件下 「HttpAsyncTask」 使用:

  1. 有參數 「get_id」, 「get_password」, 「GET_NAME」, 「get_phone」
  2. 運行時按鈕 「加盟」按

..我該怎麼辦..?

回答

0

只是建議你

  • 爲u初始化(輸出的OnCreate(..)方法的側)初始化當你setContentView(R.layout.main)被調用後不會初始化UI元素否則將給予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); 
    } 
+0

非常感謝你的建議。我解決了一個問題! – vvThat