我第一次使用Retrofit2,遇到了一些问题。
这是用于调用REST API的代码片段:
//building retrofit object
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.71:9000/api/uniapp/")
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
APIService service = retrofit.create(APIService.class);
//defining the call
Call<String> call = service.refreshAppMetaConfig("0");
//calling the api
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
//displaying the message from the response as toast
System.out.println("Uniapp :"+response);
}
@Override
public void onFailure(Call<String> call, Throwable t) {
System.out.println("Uniapp :"+t.getMessage());
}
});
这是APIService类:
public interface APIService {
//The register call
@FormUrlEncoded
@POST("appmetaconfigjson")
Call<String> refreshAppMetaConfig(@Field("versionId") String versionId);
}
我正在使用Play框架创建REST API,但是遇到了内部服务器错误。API无法读取JSON请求。但是,如果我通过Postman访问API,则会返回响应。有什么建议吗?
我已经添加了Postman请求的截图。
![Postman Sample](https://istack.dev59.com/lUMIk.webp)