0

我正在使用 Android http 东西来注册/注销服务器。我有一个使用 HttpDelete 的 DELETE 请求。当我尝试调用它时,我收到 Http401 'Bad request' 错误。我不知道为什么会这样。请帮我。

这是我的代码:

HttpUtils.java

private BasicHttpParams mParams;
private UsernamePasswordCredentials mCredentials = null;
private ResponseHandler mResponseHandler = null;

public void setUserCredentials(String userName, String password) {
     this.mCredentials = new UsernamePasswordCredentials(userName, password);
}

public void setResponseHandler(ResponseHandler responseHandler){
    this.mResponseHandler = responseHandler;
}

public Result<String> delete(String url){
    Result<String> result = new Result<T>();
    result.setStatus(Result.FAIL);

        try {
         DefaultHttpClient httpClient = new DefaultHttpClient(mParams);
         httpClient.setParams(mParams);
         httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), mCredentials);

         HttpResponse response = httpClient.execute(new HttpDelete(url));
         result.setResult(mResponseHandler.handleResponse(response));
         result.setStatus(Result.SUCCESS);
    } catch (IllegalArgumentException e) {
          e.printStackTrace();
          result.setMessage(e.getMessage());
    } catch (ClientProtocolException e) {
          e.printStackTrace();
          result.setMessage(e.getMessage());
    } catch (ConnectTimeoutException e) {
          result.setMessage("Connection timed out.");
    } catch (IOException e) {
          e.printStackTrace();
          result.setMessage(e.getMessage());
    } 

    return result;
}

UnregisterTask.java
    @Override
    protected Void doInBackground(String... urls) {
        if (urls==null || urls.length!=1)
            return null;

        String url = urls[0];

                HttpUtils httpUtils = new HttpUtils();
                httpUtils.setUserCredentials("userid", "password");
                httpUtils.setResponseHandler(new UnrgisterHandler());
        httpUtils.delete(url);

        Result<String> result = aClient.delete(url);
        if (result!=null || result.result != null){
                     //Do Something
                 }

    }


//UnrgisterActivity.java

public void onUnregisterButtonClick(View view){
     UnregisterTask task = new UnregisterTask(this);
     task.execute(ServerConfig.getIdmServer() + ServerConfig.DELETE_DEVICE + "myid");
}

收到错误:

Apache Tomcat/7.0.26 - 错误报告

HTTP 状态 400 -

类型状态报告

信息

描述客户端发送的请求语法错误()。

Apache Tomcat/7.0.26

提前致谢。

4

1 回答 1

0

我自己修复了它,但我不清楚为什么会发生错误。在搜索如何设置基本身份验证后,我更改了代码。

public Result<T> delete(String url)

        Result<T> result = new Result<T>();
        result.setStatus(Status.FAIL);

        try {
            DefaultHttpClient http = new DefaultHttpClient();

            if (this.mCredentials!=null){
                CredentialsProvider credProvider = new BasicCredentialsProvider();
                credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), this.mCredentials);
                http.setCredentialsProvider(credProvider);
            }

            HttpDelete delete = new HttpDelete(url);
            //delete.setEntity(new StringEntity(data, "UTF8"));
            delete.addHeader("Content-type", JSON_TYPE);
            HttpResponse response = http.execute(delete);
            result.setResult(mResponseHandler.handleResponse(response));
            result.setStatus(Result.Status.SUCCESS);

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
        } catch (ConnectTimeoutException e) {
            result.setMessage("Connection timed out.");
        } catch (IOException e) {
            e.printStackTrace();
            result.setMessage(e.getMessage());
        } 

        return result;
    }

还是有点混乱。无论如何,现在它的魅力。

于 2012-10-17T05:51:19.967 回答