使用 PATCH 方法的 Android REST API [英] Android REST API using PATCH method

查看:24
本文介绍了使用 PATCH 方法的 Android REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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有什么解决方案吗?

Using above code in normal post rest web service it's working fine but in PATCH api its not working giving responce status 400 meance page not found but it's working fine in Chrome rest client. Any solution for PATCH?

推荐答案

下载 httpclient-4.2.jar 文件.

Download httpclient-4.2.jar file.

导入以下文件

import org.apache.http.message.AbstractHttpMessage;导入 org.apache.http.client.methods.HttpRequestBase;导入 org.apache.http.client.methods.HttpEntityEnclosureRequestBase;导入 org.apache.http.client.methods.HttpPatch;

HttpPatch request = new HttpPatch(strUrl);

插入

HttpPost request = new HttpPost(strUrl);

这篇关于使用 PATCH 方法的 Android REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆