gpt4 book ai didi

retrofit - 使用 Retrofit 获取 Html 响应

转载 作者:行者123 更新时间:2023-12-04 18:37:47 32 4
gpt4 key购买 nike

我是改造新手。我向网站发出 POST 请求。网站以 HTML 形式返回响应。所以我会解析它。但是 Retrofit 尝试将其解析为 JSON。怎么办?

@FormUrlEncoded
@POST("/login.php?action=login")
void postCredentials(@Field("username") String username,
@Field("password") String password);

我应该使用回调吗?

最佳答案

可能不是最好的解决方案,但这是我设法通过改造获得 html 页面的来源:

MainActivity.java

ApiInterface apiService = ApiClient.getClient(context).create(ApiInterface.class);

//Because synchrone in the main thread, i don't respect myself :p
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

//Execution of the call
Call<ResponseBody> call = apiService.url();
response = call.execute();

//Decode the response text/html (gzip encoded)
ByteArrayInputStream bais = new ByteArrayInputStream(((ResponseBody)response.body()).bytes());
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);

String readed;
while ((readed = in.readLine()) != null) {
System.out.println(readed); //Log the result
}

接口(interface)接口(interface).java
@GET("/")
Call<ResponseBody> url();

ApiClient.java
public static final String BASE_URL = "https://www.google.com";

private static Retrofit retrofit = null;

public static Retrofit getClient(Context context) {
if (retrofit==null) {

OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.build();

retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}

关于retrofit - 使用 Retrofit 获取 Html 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31496666/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com