代码之家  ›  专栏  ›  技术社区  ›  Ehsan

如何处理html内容的请求?

  •  0
  • Ehsan  · 技术社区  · 8 年前

    我向服务器请求响应为 HTML 像这样键入:

    <!DOCTYPE html>
    <html>
        <head>
            <title>info starter</title>
        </head>
        <body>
            <!DOCTYPE html>
            <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <title>بازی نما</title>
                    <style>
        }
          } }
        body{margin: 0;direction: rtl; font-family: myFont;font-size: 16px;padding: 1em 2em;background: url('https://bazinama.vasapi.click/static/bazinama-files/images/boundry.jpg') top center;color: #fff;}.coins{float: left;font-size: 1.5em;margin-top: -0.2em;}.coin-img{width: 1.5em;margin: -0.1em 0.5em;float: left;animation: coin 1s infinite linear;-webkit-animation: coin 1s infinite linear;-moz-animation: coin 1s infinite linear;}.row{margin: 2em 0;}p{line-height: 2.4em !important;}.title{text-align: center;margin-bottom: 1.5em;}.title .bold{font-size: 1.5em;color: #ffc800;}.box{background: rgba(11, 111, 251, 0.9);padding: 1em;border-radius: 1em;-webkit-border-radius: 1em;-moz-border-radius: 1em;margin: 2em 0;}.mobile-icon{width: 70px;float: right;}
        </style>
                </head>
                <body>
                    <h2 class="title">
                        <span class="bold"> استارتر،</span>، پلتفرم حمایت از بازی های ایرانی و بازی سازی ملی است.
                    </h2>
                    <p>گیمرها، شرکت ها و تیم هپردازند،. </p>
                </body>
            </html>
        </body>
    </html>
    

    我把我的要求改成这样:

      @Headers("Accept: text/html")
      @GET("pages/Bazinamamag/starter/")
      Call<ResponseBody> getContactUsInfo();
    

    像这样准备请求:

    @Override
      public void getContactUsInfo(@Nullable final LoadContactUsCallback callback) {
            final ApiService service = ServiceGenerator.createService(ApiService.class);
    
            service.getContactUsInfo().enqueue(new Callback<ResponseBody>() {
              @Override
              public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
    
                try {
                  Log.d(TAG, "onResponse: " + response.body().string());
    
                  callback.onTasksLoaded(response.body().string());
    
                } catch (IOException e) {
                  e.printStackTrace();
                  Log.e(TAG, "onResponse: " + e.getMessage());
                }
    
              }
    
              @Override
              public void onFailure(Call<ResponseBody> call, Throwable t) {
    
                Log.e(TAG, "onFailure: " + t.getMessage());
                callback.onDataNotAvailable();
    
              }
            });
          }
    

    但总是不能得到 HTML格式 正在响应且为的代码。

    如何获取html响应?

    谢谢您。

    1 回复  |  直到 8 年前
        1
  •  1
  •   sschrass Maverick    8 年前

    如果您正在使用的Api只提供HTML,那么您可以告诉reformation请求如下HTML:

    @Headers("Accept: text/html")
    @GET("myUrl")
    Call<String> getContactUsInfo();
    

    另外,请注意 onResponse 响应可以是任何东西,用一个 isSuccessful 比如这里: How to get Retrofit success response status codes .

    编辑:看起来需要一个标量转换器 String 数据。 这会是

    compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
    

    如前所述 here .