1

I want to send form data to server. i m using volly library for that but this following code is showing bellow error.

Unexpected response code 405 com.android.volley.ServerError

My code is :

public class Wow extends AppCompatActivity {

    private EditText editTextName;
    private EditText editTextLocation;
    private EditText editTextEmail;
    private EditText editTextWebsite;
    private Button buttonRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_sent_form);

        editTextName = (EditText) findViewById(R.id.editTextname);
        editTextLocation = (EditText) findViewById(R.id.editTextlocation);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextWebsite = (EditText) findViewById(R.id.editTextwebsite);
        buttonRegister = (Button) findViewById(R.id.buttonRegister);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                suman();
            }
        });
    }

    void suman() {

        final String name = editTextName.getText().toString().trim();
        final String location = editTextLocation.getText().toString().trim();
        final String email = editTextEmail.getText().toString().trim();
        final String website = editTextWebsite.getText().toString().trim();

        try 
        {
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            String URL = "http://myeducationhunt.com/schools/create";
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("name", name);

            final String mRequestBody = jsonBody.toString();

            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("VOLLEY", response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY", error.toString());
                }
            }) {


                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                        return null;
                    }
                }
            };
            requestQueue.add(stringRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

**logcat looks like this **

11-15 12:49:13.648 17579-19711/com.user.retrofit E/Volley: [254] BasicNetwork.performRequest: Unexpected response code 405 for http://myeducationhunt.com/schools/create
11-15 12:49:13.649 17579-17579/com.user.retrofit E/VOLLEY: com.android.volley.ServerError
Komal12
  • 3,340
  • 4
  • 16
  • 25
seon
  • 1,050
  • 2
  • 13
  • 27

4 Answers4

2

By Changing Request GET FROM POST MY ERROR REMOVES .Thankyou for your valuable answer

Community
  • 1
  • 1
zerocool
  • 192
  • 2
  • 10
  • changing POST to GET and changing url from http://myeducationhunt.com/schools/create to http://myeducationhunt.com/schools shows Unexpected response code 500,com.android.volley.ServerError – seon Nov 15 '16 at 08:33
  • 500 cause internally better check what your are calling. its in server side not the client side – zerocool Nov 15 '16 at 08:47
  • Changing POST to GET and changing url from myeducationhunt.com/schools/create to myeducationhunt.com/school provide the data that are stored in database,but the form data are not submitted through emulator – seon Nov 15 '16 at 08:54
  • try to look in this while waiting http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put – zerocool Nov 15 '16 at 09:57
  • does decoding in server side is necessary? – seon Nov 17 '16 at 05:16
0

Try using GET method instead of POST use Request.Method.GET instead of Request.Method.POST

siddhesh
  • 563
  • 1
  • 9
  • 15
0

change your url from http://myeducationhunt.com/schools/create to http://myeducationhunt.com/schools.i think you a writing RESTful api on the server. when you use use POST method with http://myeducationhunt.com/schools url, it will automatically redirect to create function.

manichandra
  • 156
  • 1
  • 3
  • in your code, i see only name parameter that put in the JSON object.make sure the number of parameters are match between client and server.Also if you used POST method in RESTful api, there must be extra hidden field to prevent CSRF Attack. – manichandra Nov 15 '16 at 09:24
  • now i am having issue of com.android.volley.TimeoutError. – seon Nov 15 '16 at 09:34
  • check out this http://stackoverflow.com/questions/17094718/change-volley-timeout-duration – manichandra Nov 15 '16 at 09:43
0

You are missing getBodyContentType() before getBody() it. Try adding this.

@Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }
Jozef Dochan
  • 926
  • 10
  • 27