Android 不通过REST调用接收json数据
我正在尝试使用改型来使用我自己的REST调用。关于它为什么不起作用,我唯一的线索是它说信息的类型是“text/html”,尽管我确信它是json。尽管如此,我还没有找到任何解决我问题的答案 有效载荷:Android 不通过REST调用接收json数据,android,json,api,rest,retrofit,Android,Json,Api,Rest,Retrofit,我正在尝试使用改型来使用我自己的REST调用。关于它为什么不起作用,我唯一的线索是它说信息的类型是“text/html”,尽管我确信它是json。尽管如此,我还没有找到任何解决我问题的答案 有效载荷: [ { "user_id": "32", "username": "Marko", "last_activity": "2020-04-26 20:44:00", "user_image": "slika2" },
[
{
"user_id": "32",
"username": "Marko",
"last_activity": "2020-04-26 20:44:00",
"user_image": "slika2"
},
{
"user_id": "33",
"username": "dejan",
"last_activity": "2020-04-26 20:44:00",
"user_image": "slika3"
}
]
我的聊天课:
public class Chat {
@SerializedName("user_id")
private String userId;
private String username;
@SerializedName("last_activity")
private String lastActivity;
@SerializedName("user_image")
private String userImage;\
...constructor/getters
}
Api接口:
@FormUrlEncoded
@POST("api_get_all_chats.php")
Call<List<Chat>> getChats(
@Field("id") String id
);
以及整个要求:
apiInterface = ApiClient.getClient().create(userApi.class);
Call<List<Chat>> call = apiInterface.getChats(u.getUserInfo().getUserId());
call.enqueue(new Callback<List<Chat>>() {
@Override
public void onResponse(Call<List<Chat>> call, Response<List<Chat>> response) {
chatList = new ArrayList<>(response.body());
chatAdapter = new ChatAdapter(getApplication().getApplicationContext(), R.layout.user_view, chatList);
listView.setAdapter(chatAdapter);
}
@Override
public void onFailure(Call<List<Chat>> call, Throwable t) {
Toast.makeText(getApplication().getApplicationContext(), "ne valja" , Toast.LENGTH_LONG).show();
}
});
apinterface=ApiClient.getClient().create(userApi.class);
Call Call=apinterface.getChats(u.getUserInfo().getUserId());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
chatList=newarraylist(response.body());
chatAdapter=新的chatAdapter(getApplication().getApplicationContext(),R.layout.user\u视图,chatList);
setAdapter(chatAdapter);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Toast.makeText(getApplication().getApplicationContext(),“ne valja”,Toast.LENGTH_LONG).show();
}
});
任何类型的线索,如果我搞砸了,都将不胜感激。当我解码我得到的基本url时,您的基本url有空间
http://MYIP/emob projekat/api/
我建议您在
AndroidManifest
上放置不带编码的基本URL。我看到您的代码很好,我想可能是您的URL与http
方案有关的问题。您可以尝试在API接口中通过以下两种方式指定客户端对Accept:application/json
的期望响应:
@FormUrlEncoded
@标题({“Accept:application/json”})
@POST(“api\u get\u all\u chats.php”)
打电话聊天(
@字段(“id”)字符串id
);
在API客户机中,使用new reformation.Builder().addHeader(“接受”、“应用程序/json”)代码>
这个in-square/Reformation与您的非常相似(只是相反,因为在本期中,服务器不接受text/html
,而在您的案例中,它似乎是您的客户。也许,正如本期中一样,您也在使用无法解析text/html
风格响应的服务器)你需要一个这样的拦截器
public class CustomInterceptor extends Interceptor {
@override
public Response intercept(Interceptor.Chain chain) {
return chain.proceed(chain.request().newBuilder().addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build())
}
}
首先尝试记录请求,查看发送的内容,包括标题、url、正文和响应:
其次,您确定必须发出POST请求吗?GET似乎更好?尝试在POSTANI did中运行相同的请求,并且效果非常好。ip是什么?是本地主机还是普通IP还是公共IP?我会更新,但不会改变任何东西。我已经在使用同一文件夹中的一个REST调用,它可以正常工作。
public class CustomInterceptor extends Interceptor {
@override
public Response intercept(Interceptor.Chain chain) {
return chain.proceed(chain.request().newBuilder().addHeader("Content-Type", "application/json").addHeader("Accept", "application/json").build())
}
}