Search code examples
androidandroid-asynctaskget

Use values obtained from external class in HttpAsyncTask


I am trying to use values which ​​I get from an external class in doInBackground method.

I had done everything in onCreate() but, what would be the right way to get those values?

Anyways, I can also accept other way to make the get request.

Here is my code:

    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;

        }
    }
}

Solution

  • 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
    }
    
    }