我正在尝试从Android应用程序活动连接将注册页面的数据添加到我的SQL数据库中,但每当我运行它时,我都会收到此错误:
W / System.err:org.json.JSONException:类型为java.lang.String的Valu 无法转换为JSONObject
,如何解决这个问题?
这个注册类
public class RegistrationActivity extends AppCompatActivity {
private static final String TAG = "RegisterActivity";
private static final String URL_FOR_REGISTRATION = "http://192.168.1.33/arswebservices/register.php";
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
final EditText NameRegister = (EditText) findViewById(R.id.reg_UserName);
final EditText EmailRegister = (EditText) findViewById(R.id.reg_Email);
final EditText PasswordRegister = (EditText) findViewById(R.id.reg_Password);
Button registrationButton = (Button) findViewById(R.id.reg_Button);
// Progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setCancelable(false);
registrationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
registerUser(NameRegister.getText().toString(), EmailRegister.getText().toString(),
PasswordRegister.getText().toString());
}
});
}
private void registerUser(final String name, final String email, final String password) {
// Tag used to cancel the request
String cancel_req_tag = "register";
progressDialog.setMessage("Adding you ...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST,
URL_FOR_REGISTRATION, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String user = jObj.getJSONObject("user").getString("name");
Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();
// Launch login activity
Intent intent = new Intent(
RegistrationActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.getJSONObject("user").getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);
}
private void showDialog() {
if (!progressDialog.isShowing())
progressDialog.show();
}
private void hideDialog() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
答案 0 :(得分:2)
你是否使用过截击?
请参阅以下链接以获取注册表,您可能会发现它很有用 https://www.android-examples.com/volley-user-registration/
答案 1 :(得分:0)
您的错误在这里:
JSONObject jObj = new JSONObject(response);
确保在你的响应中返回一个jsonObject!
答案 2 :(得分:0)
您的错误就在这一行:
JSONObject jObj = new JSONObject(response);
尝试以下代码:
try{
JSONObject jObj = new JSONObject("{"+response+"}");
}catch (JSONException e) {
}