简体   繁体   中英

Response status code:: 404 retrofit 2.0

as far as i understand i get this issue because smth wrong with my code, but i can find exectly reason

There is my code

public class MainActivity extends AppCompatActivity {

private MyServerConnection connection;

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

    String URL = ...;
    final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    connection = retrofit.create(MyServerConnection.class);

}

interface MyServerConnection {
    @POST("/appreg")
    Call<JSONObject> postWithJson(@Body JSONObject object);
}

public void test(View view) {
    Log.e("TAG", "---!!! TEST !!!---");

    final Call<JSONObject> call = connection.postWithJson(getJson());
    call.enqueue(new Callback<JSONObject>() {
        @Override
        public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
            Log.e("Response status code: ", String.valueOf(response.code()));

            // isSuccess is true if response code => 200 and <= 300
            if (response.isSuccessful()) {
                Log.e("response body : ", response.body().toString());
            } else {
                try {
                    Log.e("response body error : ", response.errorBody().string());

             here i get NULL  --->  Log.e("!!!-- response body : ", " " + response.body());

                } catch (IOException ignored) {
                }
            }
        }

        @Override
        public void onFailure(Call<JSONObject> call, Throwable t) {
            Log.e("TAG", "---!!! ERROR !!!--- : " + t.getMessage());
        }
    });
}

private JSONObject getJson() {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("key", "val");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return jsonObject;
}

and i get such error message

E/response body error :: <!DOCTYPE html>
                                                                                        <html lang="en">
                                                                                        <head>
                                                                                          <meta http-equiv="content-type" content="text/html; charset=utf-8">
                                                                                          <title>Page not found at /appreg</title>
                                                                                          <meta name="robots" content="NONE,NOARCHIVE">
                                                                                          <style type="text/css">
                                                                                            html * { padding:0; margin:0; }
                                                                                            body * { padding:10px 20px; }
                                                                                            body * * { padding:0; }
                                                                                            body { font:small sans-serif; background:#eee; }
                                                                                            body>div { border-bottom:1px solid #ddd; }
                                                                                            h1 { font-weight:normal; margin-bottom:.4em; }
                                                                                            h1 span { font-size:60%; color:#666; font-weight:normal; }
                                                                                            table { border:none; border-collapse: collapse; width:100%; }
                                                                                            td, th { vertical-align:top; padding:2px 3px; }
                                                                                            th { width:12em; text-align:right; color:#666; padding-right:.5em; }
                                                                                            #info { background:#f6f6f6; }
                                                                                            #info ol { margin: 0.5em 4em; }
                                                                                            #info ol li { font-family: monospace; }
                                                                                            #summary { background: #ffc; }
                                                                                            #explanation { background:#eee; border-bottom: 0px none; }
                                                                                          </style>
                                                                                        </head>
                                                                                        <body>
                                                                                          <div id="summary">
                                                                                            <h1>Page not found <span>(404)</span></h1>
                                                                                            <table class="meta">
                                                                                              <tr>
                                                                                                <th>Request Method:</th>
                                                                                                <td>POST</td>
                                                                                              </tr>
                                                                                              <tr>
                                                                                                <th>Request URL:</th>
                                                                                                <td>http://52.58.65.214:3030/appreg</td>
                                                                                              </tr>

                                                                                            </table>
                                                                                          </div>
                                                                                          <div id="info">

                                                                                              <p>
                                                                                              Using the URLconf defined in <code>APIGW.urls</code>,
                                                                                              Django tried these URL patterns, in this order:
                                                                                              </p>
                                                                                              <ol>

                                                                                                  <li>




                                                                                                        ^gcm/


                                                                                                  </li>

                                                                                                  <li>

                                                                                                        ^web/


                                                                                                  </li>

                                                                                                  <li>

                                                                                                        ^app/


                                                                                                  </li>

                                                                                              </ol>
                                                                                              <p>The current URL, <code>appreg</code>, didn't match any of these.</p>

                                                                                          </div>

                                                                                          <div id="explanation">
                                                                                            <p>
                                                                                              You're seeing this error because you have <code>DEBUG = True</code> in
                                                                                              your Django settings file. Change that to <code>False</code>, and Django
                                                                                              will display a standard 404 page.
                                                                                            </p>
                                                                                          </div>
                                                                                        </body>
                                                                                        </html>

it is weird because if i put a URL in browser i retrive standard respond

{ "data": null, "service": null, "status": { "code": 1, "description": "An error occurs while processing your request, Please try later or contact our sales department", "status": "success" } }

How i can get the same standard message with my app?

What am I doing wrong?

Eventually i found the reason in my case due to /

i put this sing / in .baseUrl(URL) in the end of URL and also i put it in method @POST("/appreg") ...

When I delete it from method like this @POST("appreg") it begin to work

In retrofit 2,

when giving the / at the end of the Base URL and at the beginning of the end-point URL will produce null response 404 in the response. So, I insist to use the / at Base URL this worked for me and hopefully, it will work for you too. :)

In my case problem is @Path .

I am doing something like below.

@Headers("Content-Type:application/json")
@GET("{remaningUrl}")
Call<String> postWithJson(@Header("Authorization") String authorization,@Path String remaningUrl);

Solved by using @Url

@Headers("Content-Type:application/json")
@GET
Call<String> postWithJson(@Header("Authorization") String authorization,@Url String remaningUrl);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM