Android 无法完成改装请求?
我必须通过Android 无法完成改装请求?,android,json,retrofit,Android,Json,Retrofit,我必须通过改型以Post请求的形式发送JSON。为此,我创建了一个Json对象: private JSONObject yourJSON(){ JSONObject jsonRoot = new JSONObject(); JSONObject jsonObject1 = new JSONObject(); JSONArray jsonArray = new JSONArray(); JSONObject jsonObject2 = new JSONObje
改型以Post
请求的形式发送JSON
。为此,我创建了一个Json对象:
private JSONObject yourJSON(){
JSONObject jsonRoot = new JSONObject();
JSONObject jsonObject1 = new JSONObject();
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject2 = new JSONObject();
try{
jsonArray.put(jsonObject2);
jsonObject2.put("duration", "12");
jsonObject1.put("region", "NYC");
jsonObject1.put("or", jsonArray);
jsonRoot.put("q", jsonObject1);
jsonRoot.put("sort", "recent");
}catch (JSONException e){
e.printStackTrace();
}
return jsonRoot;
}
然后我使用这个JSON
通过以下代码发送数据
RestApiAdapter restAdapter = new RestApiAdapter();
RoomListingAPI apiservice = restAdapter.providesRestAdapter().create(RoomListingAPI.class);
JSONObject response = apiservice.getRoomListing("qwerty","application/json", yourJSON());
API方法
我的目标是发送JSON并接收JSON,但它不起作用。我在这里做错了什么。我还发送了正确的JSON
。我做错了什么?您应该在后台任务中调用您的网络操作。否则,将抛出NetworkOnMainThreadException
现在改型有两种模式。同步或异步
您使用的是同步模式,可能是在主线程中调用的
现在可以做的是更改为异步模式(使用改型回调)
也就是说,从:
public interface RoomListingAPI {
@POST("/api/listings/search")
JSONObject getRoomListing(@Header("x-parse-session-token") String token, @Header("Content-Type") String type, @Body JSONObject json);
}
致:
公共接口RoomListingAPI{
@POST(“/api/listings/search”)
void getRoomListing(@Header(“x-parse-session-token”)字符串标记、@Header(“Content-Type”)字符串类型、@Body JSONObject json、Callback responseCallback);
}
要获取JsonObject返回响应,请执行以下操作:
apiservice.getRoomListing("qwerty","application/json", yourJSON(),
new Callback<JsonObject>() {
@Override
public void success(JsonObject jsonObject, Response response) {
// jsonObject is what you're looking for
}
@Override
public void failure(RetrofitError error) {
// do something with the error
}
});
apiservice.getRoomListing(“qwerty”、“application/json”、yourJSON(),
新回调函数(){
@凌驾
public void成功(JsonObject JsonObject,响应){
//jsonObject就是您要寻找的对象
}
@凌驾
公共无效失败(错误){
//对这个错误做点什么
}
});
另外,最好使用gson库。因此,使用JsonObject而不是JsonObject。不同的信盒、不同的库、更好的性能。我不明白您面临的问题是什么。它不起作用。我看不到改装的任何反应它不起作用
意义不大<代码>我看不见
的范围从您的屏幕关闭到缺少internet权限。您在调试问题方面做了哪些工作?@njzk2抱歉。我的错!刚刚粘贴了stacktrace。顺便说一句,这是传递JSON的正确方法吗?下一步:找出错误,用谷歌搜索它,找到大量答案。我已经改为AsyncCall
,但仍然收到错误。查看编辑-3部分。它是否与JSON有关?关于这个案例有很多类似的问题。基本上,您确定返回的是JsonObject而不是字符串吗?事实上,是的,这是我得到的Json的一个实例:是的,一旦一切都正确,那么一切都应该正常。顺便说一句,你能把我的答案标记为接受吗?我正在做,但我仍然有同样的问题
public interface RoomListingAPI {
@POST("/api/listings/search")
void getRoomListing(@Header("x-parse-session-token") String token, @Header("Content-Type") String type, @Body JSONObject json, Callback<JsonObject> responseCallback);
}
apiservice.getRoomListing("qwerty","application/json", yourJSON(),
new Callback<JsonObject>() {
@Override
public void success(JsonObject jsonObject, Response response) {
// jsonObject is what you're looking for
}
@Override
public void failure(RetrofitError error) {
// do something with the error
}
});