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

点击打开链接

一个解决方案是。

在CreateUser类的开头声明 JSONObject:

 
JSONObject json;

 

在 doInBackground() 替换中:


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


使用:


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


在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();
 }


最后,你应该在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();
 }

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值