我将数据从Android客户端发送到grails后端。我想在成功发布数据时发生两件事:
但是使用我的代码,参数会成功发布到数据库,但吐司和意图不会发生。
这是我的代码:
*/
private void registerUser(final String chname, final String chdesc, final String patientID) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
Configs.URL_LOGIN, 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) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String chname = user.getString("name");
String chdesc = user.getString("description");
String patientID = user.getString("patient");
Toast.makeText(getApplicationContext(), "User successfully registered.", Toast.LENGTH_LONG).show();
// Launch login activity
Intent intent = new Intent(AddChronics.this,
ChronicsFragment.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.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 activity_register url
Map<String, String> params = new HashMap<String, String>();
params.put("name", chname);
params.put("description", chdesc);
params.put("patient", patientID);
params.put("action", "chronicAdd");
return params;
}
};
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
注意:API工作得很好。我已经检查过并且params已成功保存到db。
答案 0 :(得分:1)
你的代码看起来很好,唯一可能阻止意图和吐司发生的事情就是如果你的代码在这里得到了一个例外:
catch (JSONException e) {
e.printStackTrace();
}
由其中一个人引起:
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
你检查过了吗?
答案 1 :(得分:0)
您是否尝试过使用活动上下文而不是applicationContext?