【问题标题】:Response status code:: 404 retrofit 2.0响应状态代码:: 404 改造 2.0
【发布时间】:2016-10-19 08:02:29
【问题描述】:

据我了解,我遇到这个问题是因为我的代码有问题,但我可以找到确切的原因

这是我的代码

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

我收到这样的错误消息

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>

这很奇怪,因为如果我在浏览器中放置一个 URL,我会检索标准响应

{ "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" } }

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

我做错了什么?

【问题讨论】:

  • URL 或 RestApi 不存在........
  • 一个原因可能是您不能直接发布 json 对象。构建 hashmap 而不是 json。并使用 gson 库的 JsonElement 代替 Json
  • 你从Log.e("response body error : ", response.body().string());而不是Log.e("response body error : ", response.errorBody().string());得到什么?
  • 当您将 URL 放入浏览器时,您可能执行了 GET 命令。您的代码尝试使用 POST 命令。
  • @sushildlh 这是不可能的,因为我复制了这个 URL 并放入浏览器并检索回复 { "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" } }

标签: java android retrofit retrofit2


【解决方案1】:

由于/,最终我找到了原因

我把这首歌 / 放在 URL 末尾的 .baseUrl(URL) 中,我也把它放在方法 @POST("/appreg")...

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

【讨论】:

    【解决方案2】:

    在改造 2 中,

    当在基本 URL 的末尾和端点 URL 的开头给出 /将在响应中产生空响应 404。 所以,我坚持使用 @ 987654322@ 在 Base URL 这对我有用,希望它也对你有用。 :)

    【讨论】:

      【解决方案3】:

      就我而言,问题是 @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);
      

      【讨论】:

        猜你喜欢
        • 2021-07-11
        • 1970-01-01
        • 2015-10-26
        • 1970-01-01
        • 2018-05-16
        • 2020-09-26
        • 2017-09-26
        • 2020-09-28
        • 2018-01-27
        相关资源
        最近更新 更多