开发者

Why my HttpPut sends a GET request from my Android?

I try to connect to a REST webservice from my Android device and PUT some data there, but all I get is a GET result. Even my webserver insists it's a GET request. What is wrong with my code?

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.addHeader("Content-Type", "application/xml; charset=utf-8");
httpPut.addHeader("User-Agent", "Android");
HttpEntity entity = new StringEntity(data);
httpPut.setEntity(entity);
HttpResponse response = httpclient.execute(httpPut);
HttpEntity input = response.getEntity();
StringBuilder result = new StringBuilder();
if (input != null) {
    InputStream instream = input.getContent();
    result = convertStreamToString(instream);
    instream.close();
}
httpclient.getConnectionManager().shutdown();
retur开发者_运维知识库n result.toString();


use JSONStringer this mmight solve your problem...

i struggled a lot for this........

your code should look something like this...

HttpPut request = new HttpPut("your URL");
JSONStringer json = new JSONStringer()
.object() 
 .key("cname").value(name)
 .key("cmail").value(email)

.endObject();

StringEntity entity = new StringEntity(json.toString());
                     entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
                     entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                     request.setEntity(entity); 
                     // Send request to WCF service 
                     DefaultHttpClient httpClient = new DefaultHttpClient();

                     HttpResponse response = httpClient.execute(request); 

Thanks...


I had this happen to me with a POST request. My issue was related to a server side issue involving the combination our host's use of a proxy and mod_rewrite. Our rewrite was directing all non-www traffic to the www domain and somewhere in all that our host's proxy server converted the request to GET and dropped the post data. Our simple solution was to just use the www version in the initial request which avoids the rewrite. Didn't need to make any changes server side (which would have been tricky).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜