我正在尝试使用doInBackground方法中从外部类获取的值。
我已经在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 :(得分:1)
您可以在执行调用中传递这些值,如
HttpAsyncTask.execute(client);
然后创建一个类似
的客户端类public class Client{
public CLient(String email, String phNum){
this.email = email;
this.phNum = phNum;
}
}
在此之后,AsyncTask需要稍微改变,以便它扩展AsyncTask
答案 1 :(得分:1)
如果我是你,我会使用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/
答案 2 :(得分: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
}
}