【问题标题】:Retrofit 2 response body contents only "<html>" string改造 2 响应正文内容仅“<html>”字符串
【发布时间】:2016-05-09 12:55:21
【问题描述】:

我试图了解如何使用改造库,所以我创建了一个 android 项目和一个简单的 php 脚本。我的 index.php 文件位置是 xampp 的 htdocs 目录。

这是我的php脚本,这里我只想在脚本执行时返回一个字符串

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php  
    echo "Success";
 ?> 
 </body>
</html>

在 android 部分我想发送一个 GET 请求

@GET("/")
Call<String> test();

这里我发送响应并做必要的准备

Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                .setLenient()
                .create();        
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.43.169:80/")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        IDataSyncApi dataSyncApi = retrofit.create(IDataSyncApi.class);       
        Callback<String> callback = new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                String responseStr = gson.fromJson(response.body().toString(), String.class);
                Log.d("RESPONSE", responseStr);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        };
        Call<String> response = dataSyncApi.test();
        response.enqueue(callback);

问题是 Response 的 body 总是等于“”。我究竟做错了什么?

更新 1

我尝试使用 Wikipedia 公共 API 而不是我的,结果是一样的。 我得到了“”

 @GET("/w/api.php")
 Call<String> test(@QueryMap Map<String, String> argsMap);

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://en.wikipedia.org/")
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

Map<String, String> args = new HashMap<String, String>();
        args.put("action", "query");        
        args.put("meta", "siteinfo");
        args.put("siprop", "namespaces");
        Call<String> response = dataSyncApi.test(args);
        response.enqueue(callback);

【问题讨论】:

    标签: php android


    【解决方案1】:

    我自己想出来的。问题是我使用 String 作为 Call 类的类型 (Call&lt;String&gt;)。相反,最好使用Call&lt;ResponseBody&gt;,所以它变成了这样:

    服务

     @GET("/w/api.php")
     Call<ResponseBody> test(@QueryMap Map<String, String> argsMap);
    

    提出请求

    Call<ResponseBody> response = dataSyncApi.test(args);
            response.enqueue(callback);
    

    【讨论】:

      【解决方案2】:

      您可以使用 ResponseBody 类型。如下:

      @Multipart
      @POST("/android_upload_file/uploadfile.php")
      Call<ResponseBody> upload(@Part("fileName") String des, @Part("file\"; filename=\"1.txt\"")RequestBody file);
      

      【讨论】:

        【解决方案3】:

        您已经在Response&lt;String&gt; response 中获得了作为字符串的响应。您尝试使用用于 jsons 而不是 html 的 gson 对其进行反序列化。

        如果仍然无法正常工作,请尝试使用 HttpLoggingInterceptor 记录响应。

        【讨论】:

        • 我知道gson是用于jsons的,我并不是要反序列化html,我只是不明白为什么响应包含那个奇怪的“”标签,只有那个跨度>
        【解决方案4】:

        您确定您请求的是正确的 PHP 文档吗? 可能是 XAMPP 缓存了该文件或提供其他文件(如 index.html)?

        或者,我会更改 PHP 文件以使用 JSON 响应,并将 requestHeader 添加到您的 Retrofit 调用以接受 JSON(或 HTML,取决于您的期望)

        【讨论】:

        • 问题是我什至尝试了 Wikipedia 公共 API(请参阅问题更新),结果完全一样:我得到的是 " 而不是 " "
        猜你喜欢
        • 1970-01-01
        • 2016-10-02
        • 2017-08-07
        • 2017-04-19
        • 2015-10-22
        • 2015-04-19
        • 1970-01-01
        • 2017-04-05
        • 2016-05-31
        相关资源
        最近更新 更多