morten jonathan
morten jonathan

Reputation: 45

send data with JSON and volley

I'm trying to send data using volley and JSON to web service but I don't know how to put the data into my string request correctly

enter image description here

my code

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

error message enter image description here

Upvotes: 0

Views: 93

Answers (3)

Sachin Rajput
Sachin Rajput

Reputation: 4344

inside your code

  StringRequest stringRequest = new StringRequest(Request.Method.POST, 
  url,new Response.Listener<String>() {})

change it to

 StringRequest stringRequest = new StringRequest(Request.Method.PUT, url,  
   new Response.Listener<String>() {})

Upvotes: 1

Hồ Quốc Huy
Hồ Quốc Huy

Reputation: 95

you must send string instead json

you can send by add to hasmap like:

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

Upvotes: 0

Benjamin
Benjamin

Reputation: 421

Retrofit are more easy to use

On the first screen you'r method are PUT but in your volley request you use POST,try to change to put

Upvotes: 0

Related Questions