2014-10-20 57 views
1

我想要使用從doInBackground方法中的外部類獲得的值。使用從外部類中獲取的值在HttpAsyncTask

我已經在onCreate()中做了所有事情,但是,獲取這些值的正確方法是什麼?

無論如何,我也可以接受其他方式來獲取請求。

這裏是我的代碼:

public class MenuActivity extends Activity { 

    cUserData ud = new cUserData(this); //This is my external class 

    String email; 

    String phone; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_menu); 



     Bundle bundle = this.getIntent().getExtras(); 

     ((myApp) this.getApplication()).setSomeVariable(bundle.getString("email")); 



     cUserData ud = new cUserData(this); 

     String email= ud.getEmail; //I get the values from the external class 

     String phone = ud.getTelephone(); 


     new HttpAsyncTask().execute();  
    } 



    private class HttpAsyncTask extends AsyncTask<String, Void, String> { 

     @Override 

     protected String doInBackground(String... urls) { 

      String mEmail = email; //These values are null 

      String mPhone = phone; 

     String url = "http://www.myUrl.com/app/get.aspx?email=" + mEmail + "&telephone=" + mPhone; 

      HttpClient Client = new DefaultHttpClient(); 

      HttpGet httpget = new HttpGet(url); 

      try { 

      Client.execute(httpget); 

      } catch (ClientProtocolException e) { 

      e.printStackTrace(); 

      } catch (IOException e) { 

      e.printStackTrace(); 

      } 

      return null; 

     } 

    } 

} 
+0

http://stackoverflow.com/questions/24752274/passing-parameters-in-httpasynctask-execute – Carsten 2014-10-20 07:43:05

+0

ud.getEmail是我從另一個類調用的方法。 – imj 2014-10-20 07:46:09

+0

public String getTelephone(){ TelephonyManager telephonyManager; String context = Context.TELEPHONY_SERVICE; telephonyManager =(TelephonyManager)mContext.getSystemService(context); String mPhoneNumber = telephonyManager.getLine1Number(); return mPhoneNumber } @ Archie.bpgc – imj 2014-10-20 08:01:55

回答

1
public class MenuActivity extends Activity { 

cUserData ud = new cUserData(this); //This is my external class 

String email; 

String phone; 

@Override 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_menu); 



    Bundle bundle = this.getIntent().getExtras(); 

    ((myApp) this.getApplication()).setSomeVariable(bundle.getString("email")); 



    cUserData ud = new cUserData(this); 

    String email= ud.getEmail; //I get the values from the external class 

    String phone = ud.getTelephone(); 

    String mDataUrl ="www.myurl.com"; 
    mDataUrl += "?email=" + email; 
    mDataUrl += "&telephone=" + phone; 


    new HttpAsyncTask().execute(mDataUrl);  
} 



private class HttpAsyncTask extends AsyncTask<String, Void, String> { 

    @Override 

    protected String doInBackground(String... urls) { 
     return getData(urls[0]); 
    } 
} 

public static String getData(String dataUrl){ 
//NOW I CAN USE VALUES 
} 

} 
0

你可以通過這些價值觀在執行調用,比如

HttpAsyncTask.execute(client); 

然後創建

public class Client{ 
    public CLient(String email, String phNum){ 
     this.email = email; 
     this.phNum = phNum; 
    } 
} 

客戶端類此的AsyncTask後將需要稍微改變,以便它擴展AsyncTask

0

如果我是你,我會使用的AsyncTask類似如下:

public class MenuActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_menu); 


     Bundle bundle = this.getIntent().getExtras(); 

     ((LennyTracer) this.getApplication()).setSomeVariable(bundle.getString("email")); 

     new HttpAsyncTask().execute(new cUserData(this)); 
    } 

    private class HttpAsyncTask extends AsyncTask<cUserData, Void, String> { 

     @Override 

     protected String doInBackground(cUserData[] cUserDatas) { 

      String mEmail = cUserDatas.getEmail(); 

      String mPhone = cUserDatas.getTelephone(); 

      String url = "http://www.myUrl.com/app/get.aspx?email=" + mEmail + "&telephone=" + mPhone; 

      HttpClient Client = new DefaultHttpClient(); 

      HttpGet httpget = new HttpGet(url); 

      try { 

       Client.execute(httpget); 

      } catch (ClientProtocolException e) { 

       e.printStackTrace(); 

      } catch (IOException e) { 

       e.printStackTrace(); 

      } 

      return null; 

     } 

    } 
} 

但是也有針對更有效地處理過程異步HTTP請求許多圖書館。看看那些庫:

http://loopj.com/android-async-http/

http://projects.spring.io/spring-android/

http://developer.android.com/training/volley/index.html