Retrofit 500 内部服务器错误获取数据

Retrofit 500 Internal server error getting data

我刚开始使用 Retrofit,我试图通过 @POST 请求从服务器获取数据,但我一直 returning 500 内部服务器错误。

我的目标是通过请求发送用户名,服务器应该 return 一个 JSON 字符串,其中包含给定用户的数据列表。

我确信它很简单,因为我对使用 Retrofit 还很陌生,下面是一些代码:

我的服务生成器(使用身份验证):

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);

我的服务class:

    @FormUrlEncoded
    @POST("/GetData")
    public void GetData(@FIELD String user, Callback<List<Data>> callback);

我的主要 activity 代码:

 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 方法时,它 return 是带有请求数据的 JSON 字符串(我有一个服务器人员在后端工作,他似乎很高兴服务器是对 GetData(aUser) 命令响应良好?)。服务器看到了一些东西,但它似乎是垃圾?

感谢任何帮助。

问题已解决,问题出在服务 class 中,添加了@FIELD 注释并在方法调用中添加了@FIELD 注释和键、值对,效果很好。在上面初始 post 中编辑的完整功能代码,下面是最终修复代码:

@FormUrlEncoded
@POST("/GetData")
public void GetData(@FIELD String user, Callback<List<Data>> callback);