edittext에 받은 값을 mysql db로 insert하는 예제입니다.
ui 쓰레드에서 직접 돌리면 안된다길래 asynctask를 사용했는데요,
자꾸 오류가 뜹니다.
해당 코드와 오류 첨부하니 안드로이드 고수 분 도움 부탁드려요....
여기에 쏟은 시간이 엄청 많아요.../
*코드*
public class RegisterUser extends ActionBarActivity implements OnClickListener {
private EditText user, pass;
private Button mRegister;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://내 서버 아이피/insert.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_user);
user = (EditText) findViewById(R.id.user_id);
pass = (EditText) findViewById(R.id.user_secret);
mRegister = (Button) findViewById(R.id.save_user);
mRegister.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
new CreateUser().execute();
}
class CreateUser extends AsyncTask<String, String, String> {
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterUser.this);
pDialog.setMessage("등록 중입니다.");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
String phone = "010-1234-5678";
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", username));
params.add(new BasicNameValuePair("user_secret", password));
params.add(new BasicNameValuePair("user_phone", phone));
Log.d("request!", "starting");
// Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
params);
// json success element
success = (int)json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Created!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(RegisterUser.this, file_url, Toast.LENGTH_LONG)
.show();
}
}
}
}