意外的响应代码405 com.android.volley.ServerError

时间:2016-11-15 07:09:04

标签: android

我想将表单数据发送到服务器。我正在使用volly库,但是下面的代码显示了下面的错误。

  

意外的响应代码405 com.android.volley.ServerError

我的代码是:

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看起来像这样**

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

4 个答案:

答案 0 :(得分:1)

您调用的方法类型错误,例如从Get调用Post方法,反之亦然。尝试更改Request.Method.Post = Request.Method.Get或反之亦然

答案 1 :(得分:0)

尝试使用GET方法而不是POST 使用if ($mysqli_result = mysqli_fetch_array($query_r)) { return $mysqli_result[$get]; } 代替Request.Method.GET

答案 2 :(得分:0)

将您的网址从docker-compose更改为http://myeducationhunt.com/schools/create。我认为您在服务器上编写RESTful API。当您使用POST方法和http://myeducationhunt.com/schools url时,它会自动重定向到创建函数。

答案 3 :(得分:0)

您在getBodyContentType()之前缺少getBody()。尝试添加此内容。

@Override
    public String getBodyContentType() {
        return "application/json; charset=utf-8";
    }