开发者

How to send a request to webservice in Android?

开发者 https://www.devze.com 2023-04-04 11:05 出处:网络
I write a webservice in django if i use from terminal command like this curl --dump-header - -H \"Content-Type: application/json\" -X PUT

I write a webservice in django

if i use from terminal command like this

curl --dump-header - -H "Content-Type: application/json" -X PUT
    -开发者_运维知识库-data '{"name": "Change","address": "/api/v1/user/1/"}' 
    http://localhost:8000/api/items/1/

How to write command it to Android request to webservice?


This should do it:

HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);

HttpClient httpClient = new DefaultHttpClient(myParams);
HttpContext localContext = new BasicHttpContext();
HttpPut httpPut = new HttpPut("http://localhost:8000/api/items/1/");
httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Content-Type", "application/json");

String data = // put your JSON object here
tmp = new StringEntity(data, "UTF-8");

httpPut.setEntity(tmp);
response = httpClient.execute(httpPut, localContext);

Srting result = EntityUtils.toString(response.getEntity());
0

精彩评论

暂无评论...
验证码 换一张
取 消