我正在我的应用程序上构建一个注册区域,一切正常。
我正在使用PHP来插入,而Java用于获取值,因此,希望将值从EditTexts传递到AsynkTask类并将它们提交到我的数据库。
我该怎么做?
public class register extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
final EditText e_id=(EditText) findViewById(R.id.editText1);
final EditText e_name=(EditText) findViewById(R.id.editText2);
Button clickButton = (Button) findViewById(R.id.button1);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
insert_user mTask = new insert_user();
mTask.execute("I WOULD LIKE TO PUT THE EDITTEXT VALUE HERE");
// TODO Auto-generated method stub
}
});
}
class insert_user extends AsyncTask<String, Integer, String> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected String doInBackground(String... params) {
String passed = params[0];
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id","1"));
nameValuePairs.add(new BasicNameValuePair("name",passed));
InputStream is = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/example/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
}
String result = null;
try
{
BufferedReader reader = new BufferedReader
(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
String id = json_data.getString("id");
String name =json_data.getString("name");
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return result;
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Do things like hide the progress bar or change a TextView
}
}
}
答案 0 :(得分:0)
您可以覆盖构造函数。类似的东西:
class insert_user extends AsyncTask<String, Integer, String> {
String mText;
public insert_user(String text) {
super();
mText = text; // save the value
}
protected String doInBackground(String... params) {
// do something with mText
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id","1"));
nameValuePairs.add(new BasicNameValuePair("name",mText));
。 。
}
.
.
.
}
然后实例化并运行
insert_user mTask = new insert_user("EditText Value");
mTask.execute();
要读取e_name值:
insert_user mTask = new insert_user(e_name.getText().toString());
mTask.execute();
这将设置AsyncTask生命周期的值,包括预执行。
如果你只需要doInBackground中的String值,那么你可以直接传递一个参数数组,Android: How can I pass parameters to AsyncTask's onPreExecute()?上的精彩文章
答案 1 :(得分:0)
您传递edittext信息的方式似乎是正确的。你有撞车事吗?你可以发布logcat信息吗?
答案 2 :(得分:0)
您可以直接在doInBackGround()方法中使用这两行代码,并从这些字段中提取EditText的内容。
final EditText e_id=(EditText) findViewById(R.id.editText1);
final EditText e_name=(EditText) findViewById(R.id.editText2);
// Extracting content
String eid = e_id.getText().toString();
String ename = e_name.getText().toString();
这是最简单的解决方案。另一种选择是通过AsyncTask构造函数传递它们。