Android设置内容类型为HttpPost [英] Android set content type HttpPost

查看:292
本文介绍了Android设置内容类型为HttpPost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Android中更改HttpPost的内容类型?

How can you change the content-type of a HttpPost in android?

对于请求,我需要将内容类型设置为application / x-www-form -urlencoded

For a request I need to set the content type to application/x-www-form-urlencoded

所以我得到这个代码:

httpclient=new DefaultHttpClient();
httppost= new HttpPost(url);
StringEntity se = new StringEntity(""); 
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"));
httppost.setEntity(se);

但是,这并没有诀窍,我无法找到解决方案。

But that doesn't do the trick and I cant find the solution anywhere.

Cheers

推荐答案

            HttpPost httppost = new HttpPost(builder.getUrl());
            httppost.setHeader(HTTP.CONTENT_TYPE,
                    "application/x-www-form-urlencoded;charset=UTF-8");
            // Add your data
            httppost.setEntity(new UrlEncodedFormEntity(builder
                    .getNameValuePairs(), "UTF-8"));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

注意:构建器只包含url和namevalue对。

Note: the builder just contains url and namevalue pairs.

这篇关于Android设置内容类型为HttpPost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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