【问题标题】:POST request 403 forbidden is not supported不支持禁止 POST 请求 403
【发布时间】:2016-07-19 10:24:51
【问题描述】:

我目前正在尝试通过执行 REST 请求来获取响应 JSON,但问题是“当状态为 200:OK 时,我得到 JSON 响应,但当状态为 403:Forbidden 时,我无法获取 JSON 响应”。以下是我在 200:OK 上的工作代码,但不是任何其他状态消息:

界面:

@Rest(rootUrl = "http://something.com", converters = {FormHttpMessageConverter.class, GsonHttpMessageConverter.class})
public interface RestClient {
    @Post("/isec/api/user/login")
    JsonObject LoginIt(@Field String email, @Field String password, @Field String type);
}

用法:

JsonObject p = restClient.LoginIt(emailText.getText().toString(),passwordText.getText().toString(),spinner1.getSelectedItem().toString().toUpperCase());
            GsonStr = new Gson().toJson(p);
            ShowResults(p);

日志:

E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
                  Process: com.jutt.isec, PID: 3463
                  org.springframework.web.client.HttpClientErrorException: 403 Forbidden
                      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
                      at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:585)
                      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:541)
                      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:499)
                      at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:447)
                      at com.jutt.isec.RestClient_.LoginIt(RestClient_.java:41)
                      at com.jutt.isec.MainActivity.doLogin(MainActivity.java:130)
                      at com.jutt.isec.MainActivity_.access$201(MainActivity_.java:31)
                      at com.jutt.isec.MainActivity_$5.execute(MainActivity_.java:159)
                      at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:405)
                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
                      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                      at java.lang.Thread.run(Thread.java:818)

任何建议或解决方案?提前谢谢

【问题讨论】:

  • 如果你得到一个 403 禁止,那么你将不会得到一个 json 响应。所以不要尝试将其作为 json 处理。
  • 那么我应该如何在异常或其他情况下处理这个问题,否则我的应用程序无论如何都会崩溃
  • 将调用放在 try catch 块中。这样你的应用程序就不会崩溃。如果您的应用程序崩溃,您将在 LogCat 中找到原因。所以你知道抛出了哪个异常。告诉我们您遇到的例外情况。
  • solve5.com 是您自己的服务吗?您可以更改服务器实现,以便它以 JSON 形式返回所有 API 响应。显然,您仍然需要处理 Unauthorized -JSON,但它会是更一致的 API。
  • org.springframework.web.client.HttpClientErrorException

标签: android json rest android-annotations


【解决方案1】:

您没有收到 json,但您可以检查收到的状态。 您应该使用 RestErrorHandler,如 here 所述

我的代码示例

@EBean
public class RestErrorHandler implements RestErrorHandler {

    @RootContext
    Context context;

    @Bean
    MyErrorHandler mErrorHandler;

    @Bean
    OttoBus ottoBus;

    @Override
    public void onRestClientExceptionThrown(NestedRuntimeException e) {

        if (e instanceof HttpStatusCodeException) {
            HttpStatusCodeException exception = (HttpStatusCodeException) e;
            if (exception.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        mErrorHandler.handleGenericError(context.getString(R.string.auth_error));
            } else {
                mErrorHandler.handleGenericError(context.getString(R.string.generic_error));
            }
        } else if (e instanceof RestClientException) {
            // do something else
        } else {
            mErrorHandler.handleGenericError(context.getString(R.string.root_error));
            Log.e("errorw", e.getMessage());
        }
    }
}

您的 RestClient 必须像这样扩展 RestClientErrorHandling

public interface RestClient extends RestClientErrorHandling

然后,在你实例化你的 RestClient 的地方,你应该如下设置你的 RestErrorHandler:

@AfterInject
public void init() {
    mService.setRestErrorHandler(mErrorHandler);
}

您可以使用 HttpStatus.FORBIDDEN 更改 HttpStatus.UNAUTHORIZED

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2014-10-31
    • 2016-11-24
    • 2012-02-03
    相关资源
    最近更新 更多