【问题标题】:Android REST API using PATCH method使用 PATCH 方法的 Android REST API
【发布时间】:2015-06-15 23:12:48
【问题描述】:
 HttpClient httpClient = new DefaultHttpClient(); 

    try {
        HttpPost request = new HttpPost(strUrl);

        StringEntity params =new StringEntity(json.toString(), "UTF-8");
        //request.addHeader("content-type", "application/x-www-form-urlencoded");
        params.setContentType("application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);

        HttpEntity entity1 = response.getEntity();
        is = entity1.getContent();
        int resopnceStatus = response.getStatusLine().getStatusCode();
        AppLog.logString(TAG + "get data resopnceStatus: " + resopnceStatus);

        if (resopnceStatus != 200) {
            return "Invalid";
        }
    }catch (IllegalArgumentException timeout) {
        timeout.printStackTrace();
        return "Invalid";
    } catch (SocketTimeoutException timeout) {
        timeout.printStackTrace();
        return "Invalid";
    } catch (Exception e) {
        e.printStackTrace();
        return "Invalid";
    }

    String response = "";
    String s = "";
    try {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
        while ((s = buffer.readLine()) != null) {
            response += s;
        }
    } catch (Exception e) {
        AppLog.logString(TAG + "get data Error in Buffered: " + e.toString());
        e.printStackTrace();
    }
    AppLog.logString(TAG + "get data Return Data is: " + response);

在正常的 post rest web 服务中使用上面的代码可以正常工作,但在 PATCH api 中它无法正常工作,导致未找到响应状态 400 meance 页面,但在 Chrome rest 客户端中可以正常工作。 PATCH有什么解决办法吗?

【问题讨论】:

  • 你能在in PATCH api its not working分享更多细节
  • @nikis 什么??在 Chrome 休息客户端工作正常时,它在 android 中给了我错误
  • 太棒了。你在谈论错误而不发布它。
  • @nikis 它给出的响应码 400 意思是找不到页面,但这并不是调用 PATCH 方法的正确方法,但我不知道
  • code 400 并不代表没有找到该页面,而是表示请求错误。您可以从比较从AndroidChrome 发送的请求开始。

标签: android api rest


【解决方案1】:

下载httpclient-4.2.jar文件。

导入以下文件

import org.apache.http.message.AbstractHttpMessage; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpPatch;

HttpPatch request = new HttpPatch(strUrl);

插入

HttpPost request = new HttpPost(strUrl);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2020-10-15
    • 2015-04-12
    相关资源
    最近更新 更多