Android REST API using PATCH method

2019-03-22 09:06发布

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

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?

标签: android api rest
1条回答
beautiful°
2楼-- · 2019-03-22 09:45

Download httpclient-4.2.jar file.

import below files

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;

Put

HttpPatch request = new HttpPatch(strUrl);

Insted of

HttpPost request = new HttpPost(strUrl);
查看更多
登录 后发表回答