响应状态代码:: 404 retrofit 2.0

时间:2016-10-19 08:02:29

标签: java android retrofit retrofit2

据我所知,我得到了这个问题,因为我的代码出错了,但我可以找到正确的理由

有我的代码

auto-close=outsideClick

我收到了这样的错误消息

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

这很奇怪,因为如果我在浏览器中放入一个URL,我会回复标准回复

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>

我如何使用我的应用获得相同的标准消息?

我做错了什么?

3 个答案:

答案 0 :(得分:2)

最终,由于/

,我在我的案例中找到了原因

我在/中将此.baseUrl(URL)标记在URL的末尾,我也将其放在方法@POST("/appreg")中...

当我从像@POST("appreg")这样的方法中删除它时,它开始工作

答案 1 :(得分:1)

翻新2,

在基本URL的末尾和端点URL的末尾给出/时,将在响应中生成空响应404。因此,我坚持使用/ 位于基本URL 上对我有用,希望对您也有用。 :)

答案 2 :(得分:0)

在我的情况下,问题是 @Path

我正在做类似下面的事情。

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

使用 @Url

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