我想通过Retrofit发送字符串作为get请求,并将响应作为字符串获取。为了更清楚,我不想使用Json。
发送数据示例:
响应数据示例:
dsa<#12no密码; +;#yes;
接口:
@GET("/deneme.aspx")
Call<ServerResponse> get(
@Query("") String method
);
我添加了ScalarsConverterFactory
但不允许我只使用字符串。
public void loginGet(String strg_no){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(SERVER_URL)
.build();
Log.d("SIGNAL", "strg_no: " + strg_no);
Interface service = retrofit.create(Interface.class);
Call<ServerResponse> call = service.get(strg_no);
call.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
// response.isSuccessful() is true if the response code is 2xx
BusProvider.getInstance().post(new ServerEvent(response.body()));
Log.d("SIGNAL", "response body : " + response.body());
Log.e(TAG,"Success");
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
// handle execution failures like no internet connectivity
BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
}
});
}
服务器响应:
public class ServerResponse {
@SerializedName("returned_ strg_no")
private String strg_no;
@SerializedName("error_code")
private int errorCode;
private int status = 1;
private String error;
public ServerResponse(String strg_no, int errorCode, int status, String error){
this. strg_no = strg_no;
this.errorCode = errorCode;
this.status = status;
this.error = error;
}
public String getstrg_no() {
return strg_no;
}
}
服务器事件:
public class ServerEvent {
private ServerResponse serverResponse;
public ServerEvent(ServerResponse serverResponse) {
this.serverResponse = serverResponse;
}
public ServerResponse getServerResponse() {
return serverResponse;
}
public void setServerResponse(ServerResponse serverResponse) {
this.serverResponse = serverResponse;
}
}
如何在不使用Json的情况下发送和获取数据?
答案 0 :(得分:1)
我知道它会对你有帮助!
@GET("/deneme.aspx")
Call<ResponseBody> get(
@Query("") String method
);
Call<ResponseBody> call = service.get(strg_no);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
// handle success
String result = response.body().string();
}
@Override
public void onFailure(Throwable t) {
// handle failure
}});
答案 1 :(得分:1)
尝试使用@Path
代替@Query
@GET("/deneme.aspx{query}")
Call<ServerResponse> get(@Path("query") String method);
并像这样打电话
Call<ServerResponse> call = service.get("?"+strg_no);
或
尝试使用动态网址
@GET
Call<ServerResponse> get(@Url String url);
并像这样打电话
Call<ServerResponse> call = service.get("http://192.168.1.2:51276/deneme.aspx?"+strg_no);