我正在尝试使用Volley和JSON将数据发送到Web服务,但是我不知道如何将数据正确地放入我的字符串请求中
JsonConvert.DeserializeObject
overload
我的代码
String url = "http://api.jasamedika.co.id/service/pegawai/1200034";
final ProgressDialog loading = ProgressDialog.show(getContext(), "Uploading...", "Please wait...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e(TAG, "Response: " + response.toString());
try {
JSONObject jObj = new JSONObject(response);
succes = jObj.getInt(TAG_SUCCESS);
if (succes == 1) {
Toast.makeText(getContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
//kosong();
} else {
Toast.makeText(getContext(), jObj.getString(TAG_MESSAGE), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
//menghilangkan progress dialog
loading.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//menghilangkan progress dialog
loading.dismiss();
//menampilkan toast
Toast.makeText(getContext(), error.toString(), Toast.LENGTH_LONG).show();
Log.e(TAG, error.getMessage().toString());
}
}) {
@Override
protected Map<String, String> getParams() {
//membuat parameters
Map<String, String> params = new HashMap<String, String>();
//menambah parameter yang di kirim ke web servis
params.put("Content-Type", "application/json");
params.put("ID",id.getText().toString().trim());
params.put("Nama", nama.getText().toString().trim());
params.put("Jenis_Kelamin", jenis_kelamin.getText().toString().trim());
params.put("Tgl_Lahir",tgl_lahir.getText().toString().trim() );
params.put("ID_Jabatan",id_jabatan.getText().toString().trim());
params.put("Salary",salary.getText().toString().trim());
//kembali ke parameters
Log.e(TAG, "" + params);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest, tag_json_obj);
}
答案 0 :(得分:1)
StringRequest stringRequest = new StringRequest(Request.Method.POST,
url,new Response.Listener<String>() {})
StringRequest stringRequest = new StringRequest(Request.Method.PUT, url,
new Response.Listener<String>() {})
答案 1 :(得分:0)
翻新版更易于使用
在第一个屏幕上,您的方法是PUT,但在排球请求中使用POST,请尝试更改放置位置
答案 2 :(得分:0)
您必须发送字符串而不是json
您可以通过添加到hasmap发送,例如:
params.put("ID",id.getText().toString().trim());
params.put("user[Nama]", nama.getText().toString().trim());
params.put("user[Jenis_Kelamin]", jenis_kelamin.getText().toString().trim());
params.put("user[Tgl_Lahir]",tgl_lahir.getText().toString().trim() );
params.put("user[ID_Jabatan]",id_jabatan.getText().toString().trim());
params.put("user[Salary]",salary.getText().toString().trim());
enter code here