使用PATCH方法的Android REST API

时间:2015-04-10 09:31:57

标签: android api rest

 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);

在普通的休息后网络服务中使用上面的代码它运行正常,但在PATCH api中它没有工作,没有找到响应状态400 meance页面,但它在Chrome休息客户端正常工作。 PATCH的任何解决方案?

1 个答案:

答案 0 :(得分:0)

下载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);

Insted of

HttpPost request = new HttpPost(strUrl);