【发布时间】:2020-05-21 20:50:18
【问题描述】:
我正在尝试使用 volley 发送数据。
我创建了一个应用程序,它首先检查所有数据,然后使用 volley 方法将数据发送到服务器,但是一旦我单击按钮上传活动,我的应用程序就会崩溃,如果我正确发送数据,任何人都可以帮助我格式与否。
这是我想以表格形式发送到我的服务器的代码:
{
"member_Details":[
{
"memberID": "1",
"memberName": "Paddy",
"mobile": "1",
"telephone": "07",
"email": "03",
},
{
"memberID": "1",
"memberName": "Paddy",
"mobile": "1",
"telephone": "07",
"email": "03",
}
],
"FarmerRegID":"130"
}
我尝试过的代码:
private void uploading_data() {
String URL = "my url";
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("response_login", response);
try {
JSONObject json = new JSONObject(response);
int success = json.getInt("success");
String msg = json.getString("message");
if (success == 1) {
finish();
} else {
Toast.makeText(Register2Activity.this, msg, Toast.LENGTH_SHORT).show();
}
} catch (Exception ex) {
Log.i("Expection", ex.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error: " + error.getMessage());
showServerConnectionError();
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("memberID", member_id);
params.put("memberName", name);
params.put("mobile", mobile);
params.put("telephone", telephone);
params.put("email", email);
return params;
}
};
RequestQueue queue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
queue.add(jsonObjRequest);
}
private void showServerConnectionError() {
AlertDialog.Builder builder = new AlertDialog.Builder(Register2Activity.this);
builder.setMessage("Can't establish a connection to server,Please try again!")
.setTitle("Internet Or Server connection Error !");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog.show();
}
【问题讨论】:
-
是否有任何错误只是因为我必须先在数组中发送数据然后将其保存在多个对象中??
-
在应用崩溃时发布您的堆栈跟踪
标签: java android android-volley