这似乎是一个非常简单的问题,但我无法弄清楚。我调用的API在Postman中工作正常,并构成了App的iOS版本。只是Android让我很健康。可能还不够咖啡。我正在使用Android Studio进行最新更新。
API接受一个长URL作为JSON主体,并返回一个shortURL作为字符串。
错误屏幕截图:
发布电话:
xmlstarlet ed -L ...
RetrofitInterface.java
private void getShortURL() {
//Get full url
final String fullURL = Constants.SHARE_BASE_URI + urlParameters;
//Convert URL to JSON formatted data
Map<String,String> data = new HashMap<String,String>();
data.put("URL",fullURL);
String jsonData = new GsonBuilder().create().toJson(data,Map.class);
//Call API and pass in JSON to get Short URL
Call<ResponseBody> getDynamicLinkCall = RetrofitAdapter.getRetroInterface(Constants.BASE_URI).getDynamicLink_String(Constants.getTimestamp(),Constants.APPLICATION,"application/json", jsonData);
getDynamicLinkCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response == null || response.code() != Constants.SUCCESS) {
//Share Short URL
share(response.body().toString());
}
else
{
//If Short URl Failed share long URL
share(fullURL);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//If Short URl Failed share long URL
share(fullURL);
}
});
}
邮差截图:
Screenshot from Postman showing Body settings
改装适配器代码:
@POST("URLShortner")
Call<ResponseBody> getDynamicLink_String(@Header("TimeStamp") String timestamp, @Header("Application") String application, @Header("Content-Type") String contentType, @Body String jsonData);
我错过了什么?
提前致谢!
答案 0 :(得分:0)
好的,我们发现了这个问题。我们错过了以下代码行:
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonObject.toString());
此代码在POST调用之前进入,而不是发布jsonData,我们发布了修复它的正文。希望Android Studio提供某种错误来暗示这一点。
完整方法:
private void getShortURL() {
//Get full url
final String fullURL = Constants.SHARE_BASE_URI + urlParams;
//Convert URL to JSON formatted data
Map<String,String> data = new HashMap<String,String>();
data.put("LINK",fullURL);
String jsonData = new GsonBuilder().create().toJson(data,Map.class);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonData);
//Call API and pass in JSON to get Short URL
Call<String> getDynamicLinkCall = RetrofitAdapter.getRetroInterface(Constants.BASE_URI).getDynamicLink_String(Constants.getTimestamp(),Constants.APPLICATION,"application/json", body);
getDynamicLinkCall.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response == null || response.code() != Constants.SUCCESS) {
//Share Short URL
share(response.body().toString());
}
else
{
//If Short URl Failed share long URL
share(fullURL);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
//If Short URl Failed share long URL
share(fullURL);
}
});
}
<强> RetrofitINterface.java:强>
@POST("URLShortner")
Call<String> getDynamicLink_String(@Header("TimeStamp") String timestamp, @Header("Application") String application, @Header("Content-Type") String contentType, @Body RequestBody jsonData);
全部谢谢!