【问题标题】:Retrofit 500 Internal server error getting data改造 500 内部服务器错误获取数据
【发布时间】:2015-10-30 21:41:09
【问题描述】:

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

我的目标是通过请求发送用户名,服务器应该返回一个 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);

我的服务等级:

    @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) 命令响应良好?) .服务器看到了一些东西,但它看起来像垃圾?

任何帮助表示赞赏。

【问题讨论】:

  • 你从服务器得到错误日志了吗?
  • 我还没有实际的日志,但很快就会上传,但日志显示以 .asmx 结尾的 URL 没有后续内容?我刚刚更新了服务类(见上文),但没有成功 500 错误仍然存​​在。
  • 我可能是错的,但你设置你的 URL 然后设置:@POST("/ProjectZWS/projectzWebService.asmx") 你的服务方法名称在哪里?
  • 服务名称是否在我上面的主要活动代码中设置为“服务”(参见上面的主要活动代码)?
  • 我不明白,您已经在服务中创建了 GetData,但我没有看到您要调用的方法。还有你在打电话 service.GetReviews?

标签: android json http-post retrofit internal-server-error


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2016-09-27
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多