Call<JsonObject> mService = mApiInterface.getAllComplaints(userToken, Constants.HEADER_ACCEPT, userId);
如何将Call转换为Json?
答案 0 :(得分:0)
假设其POST
请求,请执行改进2 call
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("enter_base_url")
.addConverterFactory(GsonConverterFactory.create())
.build();
SampleInterface request = retrofit.create(SampleInterface.class);
Call<JsonObject> call1=request.getAllComplaints(userToken, Constants.HEADER_ACCEPT, userId);
call1.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
Toast.makeText(MainActivity.this,response.body().toString(),Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Toast.makeText(MainActivity.this,t.toString(),Toast.LENGTH_SHORT).show();
}
});
将"userToken", "Constants.HEADER_ACCEPT", "userId"
替换为必填字段,同时添加&#34; base url&#34;
<强> SampleInterface.java 强>
public interface SampleInterface {
@POST("add_endpoint")
Call<JsonObject> getAllComplaints(@Body String BuserToken,@Body String asjh,@Body String userId);
}
在SampleInterface
中添加add_endpoint
如果是 GET 请求,您应该使用{{1}更改SampleInterface
以接受@Query
或@Path
参数(根据需要)注释
*********** 编辑 *********
像这样制作jsonobject请求
@GET
然后这样称呼
JsonObject jsonObject=new JsonObject();
jsonObject.addProperty("user_token","");
jsonObject.addProperty("user_name","");
jsonObject.addProperty("user_id","");
然后
Call<JsonObject> mService = mApiInterface.getAllComplaints(jsonObject);