Error 405 when using Httppost on Android

Asked

Viewed 141 times

3

By making a post to the server via Httppost Android get a 405 error. Other applications do the same post successfully, only when part of Android I get this error. Someone knows the reason why?

My code on Android is:

private class sendSignUpPost extends AsyncTask<String, Void, String> {

ProgressDialog dialog = new ProgressDialog(SignUp.this);

@Override
protected void onPreExecute() {
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setCancelable(false);
    dialog.setMax(100);
    dialog.setMessage(getString(R.string.text_check_data));
    dialog.show();
    checkConnection();
    //checkFields();
}

@Override
protected String doInBackground(String... params) {

    dialog.setMessage(getString(R.string.text_creating_account));
    dialog.setProgress(75);

    JSONObject jsonObjSend = new JSONObject();
    try {
        jsonObjSend.put("action", "signup");
        jsonObjSend.put("firstname", FIRSTNAME);
        jsonObjSend.put("lastname", LASTNAME);
        jsonObjSend.put("email", EMAIL);
        jsonObjSend.put("password", PASSWORD);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://nerd.com.br/");
    httppost.setHeader("User-Agent", "br.com.nerd");
    httppost.setHeader("Accept", "application/json");
    httppost.setHeader("Content-Type", "application/json");
    StringEntity se = null;
    try {
        se = new StringEntity(jsonObjSend.toString());
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    httppost.setEntity(se);
    HttpResponse response = null;

    try {
        response = httpclient.execute(httppost);
        System.out.println("=============>" + httppost);
    } catch (ClientProtocolException e) {
        Toast.makeText(getApplicationContext(), R.string.text_check_network, Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }



    BasicResponseHandler responseHandler = new BasicResponseHandler();
    String strResponse = null;
    if (response != null) {
        try {
            strResponse = responseHandler.handleResponse(response);
            System.out.println("=============>" + response);
        } catch (HttpResponseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return strResponse;
}
  • Strange... This code doesn’t seem to have any problem. It has the possibility to change the responseHandler.handleResponse(response); for EntityUtils.toString(response.getEntity());. I made a test with this exchange and I get a 404 Cannot POST / (Of course I put false data in JSON). I put my code in this Gist: https://gist.github.com/wakim/3c5bc2045f248e48a64e. I ran the same request using http://www.hurl.it/ and it was the same 404.

  • Hello @Wakim changed but nothing.

  • Still with 405? Testing through the site too (on desktop) or just on mobile?

  • Still with 405, very strange, only by Android, testing on the Desktop works perfectly. I did a post by Chrome’s Advanced Rest Client plugin and it works. Posts from other sites also work.

  • I don’t know what it might be... I hope someone else can help you. One last attempt would be to use another Stack to make this request... If you do not mind using or the OkHttp or the Volley or another library that requests... Just to check if it fixes the problem. I made another code using the AndroidHttpClient which is a wrapper of HttpClient, I don’t know how much will change: https://gist.github.com/wakim/3c5bc2045f248e48a64e.

  • 1

    With Androidhttpclient still having the same problem, I’ll take your advice and try it with Okhttp and Volley. Thanks a lot for the help!

  • All right, I found that the problem was related to the domain that had not been propagated. @Wakim, thank you very much!

Show 2 more comments

1 answer

0

I will put the answer here so that others can see that the question has been answered:

" the problem was related to the domain that had not been propagated. "

Browser other questions tagged

You are not signed in. Login or sign up in order to post.