我刚刚开始使用Retrofit,我正在尝试通过@POST请求从服务器获取数据,但我一直在返回500内部服务器错误。
我的目标是通过请求发送用户名,服务器应该返回一个JSON字符串,其中包含给定用户的数据列表。
我确信它很简单,因为我使用Retrofit相当新鲜,下面是一些代码:
我的serviceGenerator(使用身份验证):
private static final String URL = "http://IP address/ProjectZWS/projectzWebService.asmx";
private static RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(URL)
.setClient(new OkClient(new OkHttpClient()));
public static <S> S createService(Class<S> serviceClass, String username, String password) {
if (username != null && password != null) {
// concatenate username and password with colon for authentication
String credentials = username + ":" + password;
// create Base64 encodet string
final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
builder.setRequestInterceptor(new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
request.addHeader("Authorization", basic);
request.addHeader("Accept", "application/x-www-form-urlencoded");
}
});
}
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);
我的服务类:
@FormUrlEncoded
@POST("/GetData")
public void GetData(@FIELD String user, Callback<List<Data>> callback);
我的主要活动代码:
InstituteService service = RestService.createService(InstituteService.class, "user", "pass");
service.GetData("aUser","",new Callback <List<Data>>() {
@Override
public void success(List<Data> data, Response response) {
Toast.makeText(ctx, "success!!!!", Toast.LENGTH_SHORT).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(ctx, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
服务器日志:
2015-10-31 10:47:02 192.168.1.14 POST /ProjectZWS/projectzWebService.asmx/GetReviews - 80 jeff 176.61.63.95 okhttp/2.5.0 - 500 0 0 1328
索取信息:
Request URL: http://32.17.47.56/ProjectZWS/projectzWebService.asmx/
Request path: /ProjectZWS/projectzWebService.asmx/
User host address: 176.61.65.876
User: PC\the pc
Is authenticated: True
Authentication Type: Basic
Thread account name: IIS APPPOOL\ASP.NET v4.0 Classic
线程信息:
Thread ID: 10
Thread account name: IIS APPPOOL\ASP.NET v4.0 Classic
Is impersonating: False
Stack trace: at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
处理信息:
Process ID: 3288
Process name: w3wp.exe
Account name: IIS APPPOOL\ASP.NET v4.0 Classic
例外信息:
Exception type: InvalidOperationException
Exception message: Invalid web service call, expected path info of /js/<Method>.
at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
服务器是ASMX,当服务器发送GetData方法时,它返回带有请求数据的JSON字符串(我有一个服务器人工作后端,他似乎很高兴服务器响应GetData(aUser)命令?)。服务器看到的东西,但它似乎是垃圾?
任何帮助赞赏。
问题已解决,问题出现在服务类中,添加了@FIELD注释并在方法调用中添加了@FIELD注释和键值对,它起作用了。在上面的初始帖子中编辑的全功能代码,下面是最终修复代码:
@FormUrlEncoded
@POST("/GetData")
public void GetData(@FIELD String user, Callback<List<Data>> callback);