您的位置:首页 > 移动开发 > Android开发

如何从android应用向MYsql服务器发送数据?

2017-10-20 15:03 302 查看
点击打开链接

一个解决方案是。

在CreateUser类的开头声明 JSONObject:

[code]
JSONObject json;

[/code]
在 doInBackground() 替换中:

JSONObject json = jsonParser.makeHttpRequest(url_new_user,
"POST", params);

[/code]
使用:

json = jsonParser.makeHttpRequest(url_new_user,
"POST", params);

[/code]
在onPostExecute中移动这里代码:

try {
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
//successfully created a user
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
//closing this screen
finish();
} else {
//failed to create user
Log.d("failed to create user", json.toString());

}
} catch (JSONException e) {
e.printStackTrace();
}

[/code]
最后,你应该在CreateUser类中具有这里属性:


class CreateUser extends AsyncTask<String, String, String> {
JSONObject json;

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ConsentActivity.this);
pDialog.setMessage("Registering New User..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected String doInBackground(String... args) {

SharedPreferences pref = getSharedPreferences("UserMgmt", Context.MODE_PRIVATE);

String Firstname = pref.getString("FName","");
String Lastname = pref.getString("LName","");
String Userid = pref.getString("UserId","");
String Password = pref.getString("Password","");
String Repassword = pref.getString("RePassword","");

String Idproof_typ=pref.getString("IdType","");
String Idproof_number=pref.getString("IdNumber","");
String Dob=pref.getString("DOB","");
String Gender=pref.getString("Gender","");
String Email=pref.getString("Email","");
String Mobile_number=pref.getString("Mobile","");
String Country_code=pref.getString("CountryCode","");
String Landline=pref.getString("LandLine","");

String Country=pref.getString("Country","");
String State=pref.getString("State","");
String Address=pref.getString("Address","");
String Pincod=pref.getString("Pincode","");

boolean Tc_chk_state = pref.getBoolean("ChkBoxTC", true);

boolean Consent_chk_state = pref.getBoolean("ChkBoxConsent", true);

List<NameValuePair> params = new ArrayList<NameValuePair>();

//params.add(new BasicNameValuePair("userId", Userid));
params.add(new BasicNameValuePair("firstName", Firstname));
params.add(new BasicNameValuePair("lastName", Lastname));
params.add(new BasicNameValuePair("password", Password));
params.add(new BasicNameValuePair("newPassword", Repassword));

params.add(new BasicNameValuePair("identityType", Idproof_typ));
params.add(new BasicNameValuePair("identityNumber", Idproof_number));
params.add(new BasicNameValuePair("dob", Dob));
params.add(new BasicNameValuePair("gender", Gender));
params.add(new BasicNameValuePair("emailId", Email));
params.add(new BasicNameValuePair("mobileNumber", Mobile_number));
params.add(new BasicNameValuePair("stdCode", Country_code));
params.add(new BasicNameValuePair("landlineNumber", Landline));
params.add(new BasicNameValuePair("countryCode", Country));
params.add(new BasicNameValuePair("stateCode", State));
params.add(new BasicNameValuePair("addr", Address));
params.add(new BasicNameValuePair("zipcode", Pincod));
json = jsonParser.makeHttpRequest(url_new_user, "POST", params);//Log.d("Create Response", json.toString());

return null;
}

protected void onProgressUpdate(Integer... progress) {

}

protected void onPostExecute(String file_url) {
//dismiss the dialog once done
pDialog.dismiss();try { int success = json.getInt(TAG_SUCCESS); if (success == 1) { //successfully created a user Intent i = new Intent(getApplicationContext(), LoginActivity.class); startActivity(i); //closing this screen finish(); } else { //failed to create user Log.d("failed to create user", json.toString()); } } catch (JSONException e) { e.printStackTrace(); }}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: