如何在 Android 中设置选项 Header 和 HTTP POST 请求中的内容
How to set the option Header and Content in HTTP POST Resquest in Android
我在 Android 中使用了 HttpPost 和 HttpURLConnection 来发送带有以下 Headers 和内容的请求:
Headers:
api_key: 9a8akx8badkaxxxx,
速度:0,
声音:男,
韵律:1,
Cache-Control: no-cache
内容(Content-Type:text/plain):
大家好
这些是我的代码:
*使用 HttpPost:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(requestURL);
post.addHeader("api_key", "9a8akx8badkaxxxx");
post.addHeader("speed", "0");
post.addHeader("voice", "male");
post.addHeader("prosody", "1");
post.addHeader("Cache-Control", "no-cache");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data[body]", "Hello everyone"));
AbstractHttpEntity ent= null;
ent = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
ent.setContentEncoding("UTF-8");
post.setEntity(ent);
post.setURI(new URI("http://api.openfpt.vn/text2speech/v4"));
HttpResponse response =client.execute(post);
*使用 HttpURLConnection:
URL url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("api_key", "9a8akx8badkaxxxx");
conn.setRequestProperty("Content-Type", "text/plain");
conn.setRequestProperty("speed", "0");
conn.setRequestProperty("voice", "male");
conn.setRequestProperty("prosody", "1");
conn.setRequestProperty("Cache-Control", "no-cache");
然后不知道把内容"Hello everyone"放入HttpURLConnection
的方法
结果总是出错。但是当我在 Firefox 中使用 add-on HttpRequester 时,响应正常。
请帮助我在 Android
中创建和设置 Http 请求
试试这个来设置 content-type:
HttpPost httppost = new HttpPost(builder.getUrl());
httppost.setHeader(HTTP.CONTENT_TYPE,
"text/plain;charset=UTF-8");
要任意设置headers,我觉得应该是这样的:
httppost.setHeader("api_key", "9a8akx8badkaxxxx");
httppost.setHeader("speed", "0");
httppost.setHeader("voice", "male");
httppost.setHeader("voice", "male");
httppost.setHeader("prosody", "1");
httppost.setHeader("Cache-Control", "no-cache");
要发送数据,请为您的 post 字段选择一个名称(此处为 Fn 和 IdDevice):
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
// nameValuePairs.add(new BasicNameValuePair("Fn",function));
// nameValuePairs.add(new BasicNameValuePair("IdDevice",IdDevice));
编辑开始
nameValuePairs.add(new BasicNameValuePair("Content","Hello everyone"));
编辑结束
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
最后,执行 post 请求:
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
我在 Android 中使用了 HttpPost 和 HttpURLConnection 来发送带有以下 Headers 和内容的请求:
Headers: api_key: 9a8akx8badkaxxxx, 速度:0, 声音:男, 韵律:1, Cache-Control: no-cache
内容(Content-Type:text/plain): 大家好
这些是我的代码: *使用 HttpPost:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(requestURL);
post.addHeader("api_key", "9a8akx8badkaxxxx");
post.addHeader("speed", "0");
post.addHeader("voice", "male");
post.addHeader("prosody", "1");
post.addHeader("Cache-Control", "no-cache");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data[body]", "Hello everyone"));
AbstractHttpEntity ent= null;
ent = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
ent.setContentEncoding("UTF-8");
post.setEntity(ent);
post.setURI(new URI("http://api.openfpt.vn/text2speech/v4"));
HttpResponse response =client.execute(post);
*使用 HttpURLConnection:
URL url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("api_key", "9a8akx8badkaxxxx");
conn.setRequestProperty("Content-Type", "text/plain");
conn.setRequestProperty("speed", "0");
conn.setRequestProperty("voice", "male");
conn.setRequestProperty("prosody", "1");
conn.setRequestProperty("Cache-Control", "no-cache");
然后不知道把内容"Hello everyone"放入HttpURLConnection
的方法结果总是出错。但是当我在 Firefox 中使用 add-on HttpRequester 时,响应正常。 请帮助我在 Android
中创建和设置 Http 请求试试这个来设置 content-type:
HttpPost httppost = new HttpPost(builder.getUrl());
httppost.setHeader(HTTP.CONTENT_TYPE,
"text/plain;charset=UTF-8");
要任意设置headers,我觉得应该是这样的:
httppost.setHeader("api_key", "9a8akx8badkaxxxx");
httppost.setHeader("speed", "0");
httppost.setHeader("voice", "male");
httppost.setHeader("voice", "male");
httppost.setHeader("prosody", "1");
httppost.setHeader("Cache-Control", "no-cache");
要发送数据,请为您的 post 字段选择一个名称(此处为 Fn 和 IdDevice):
List<NameValuePair> nameValuePairs = new ArrayList<>(1);
// nameValuePairs.add(new BasicNameValuePair("Fn",function));
// nameValuePairs.add(new BasicNameValuePair("IdDevice",IdDevice));
编辑开始
nameValuePairs.add(new BasicNameValuePair("Content","Hello everyone"));
编辑结束
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
最后,执行 post 请求:
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);