Java HttpClient到Httpurlconnection的转换
我在以前的应用程序中使用过httpclient,它们工作得很好。然而,由于httpclient现在在最新的android版本中被去除了污点,您能帮我处理这段特殊的代码吗?我已经尝试了很多把它转换成httpurlconnection它对我根本不起作用Java HttpClient到Httpurlconnection的转换,java,android,http,httpclient,httpurlconnection,Java,Android,Http,Httpclient,Httpurlconnection,我在以前的应用程序中使用过httpclient,它们工作得很好。然而,由于httpclient现在在最新的android版本中被去除了污点,您能帮我处理这段特殊的代码吗?我已经尝试了很多把它转换成httpurlconnection它对我根本不起作用 HttpClient httpClient = new DefaultHttpClient(); // Creating HTTP Post HttpPost httpPost = new HttpPost(link); //Name valu
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(link);
//Name value pair created what you want to send to server
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("Languages",lang));
nameValuePair.add(new BasicNameValuePair("voice",vc));
nameValuePair.add(new BasicNameValuePair("ex","execute"));
nameValuePair.add(new BasicNameValuePair("op",text));
nameValuePair.add(new BasicNameValuePair("count",count));
nameValuePair.add(new BasicNameValuePair("speed",speed));
//httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8")); // UTF 8 added to send indic string to server
HttpResponse httpResponse = httpClient.execute(httpPost);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
Log.d("Response Status Code",String.valueOf(statusCode));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient-HttpClient=newdefaulthttpclient();
//创建HTTP Post
HttpPost HttpPost=新的HttpPost(链接);
//名称-值对创建了要发送到服务器的内容
List nameValuePair=新的ArrayList(2);
添加(新的BasicNameValuePair(“语言”,lang));
添加(新的BasicNameValuePair(“voice”,vc));
添加(新的BasicNameValuePair(“ex”、“execute”);
添加(新的BasicNameValuePair(“op”,文本));
添加(新的BasicNameValuePair(“count”,count));
添加(新的BasicNameValuePair(“速度”,speed));
//setEntity(新的UrlEncodedFormEntity(nameValuePair));
httpPost.setEntity(新的UrlEncodedFormEntity(nameValuePair,“UTF-8”);//添加UTF 8以向服务器发送索引字符串
HttpResponse HttpResponse=httpClient.execute(httpPost);
final int statusCode=httpResponse.getStatusLine().getStatusCode();
Log.d(“响应状态代码”,String.valueOf(statusCode));
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
这是我为httpurlconnection编写的代码:
URL url = new URL(link);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setInstanceFollowRedirects(false);
urlConnection.setRequestMethod("POST");
Map<String, String> param = new LinkedHashMap<String,String>();
param.put("Languages", lang);
param.put("voice",vc);
param.put("ex","execute");
param.put("op",nt);
param.put("count",count);
param.put("speed",speed);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,String> param1 : param .entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param1.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param1.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
int postDataLength = postDataBytes.length;
urlConnection.setRequestProperty( "charset", "utf-8");
urlConnection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
urlConnection.setUseCaches(false);
urlConnection.getOutputStream().write(postDataBytes);
Log.d("getoutputstream", postDataBytes.toString());
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// readStream(in);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
str = br.readLine();
Log.d("output of str",str);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
URL=newurl(链接);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod(“POST”);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setInstanceFlowRedirects(false);
urlConnection.setRequestMethod(“POST”);
Map param=新建LinkedHashMap();
参数put(“语言”,lang);
参数put(“语音”,vc);
参数出售(“ex”、“execute”);
参数put(“op”,nt);
参数put(“计数”,计数);
参数put(“速度”,速度);
StringBuilder postData=新建StringBuilder();
对于(Map.Entry param1:param.entrySet()){
如果(postData.length()!=0)postData.append('&');
append(URLEncoder.encode(param1.getKey(),“UTF-8”);
postData.append('=');
append(URLEncoder.encode(String.valueOf(param1.getValue()),“UTF-8”);
}
字节[]postDataBytes=postData.toString().getBytes(“UTF-8”);
int postDataLength=postDataBytes.length;
setRequestProperty(“字符集”、“utf-8”);
setRequestProperty(“内容长度”,Integer.toString(postDataLength));
urlConnection.setUseCaches(false);
urlConnection.getOutputStream().write(postDataBytes);
Log.d(“getoutputstream”,postDataBytes.toString());
InputStream in=new BufferedInputStream(urlConnection.getInputStream());
//readStream(in);
InputStreamReader isr=新的InputStreamReader(in);
BufferedReader br=新的BufferedReader(isr);
str=br.readLine();
Log.d(“str的输出”,str);
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}最后{
urlConnection.disconnect();
}
请告诉我我做错了什么?提前谢谢 请告诉我们“根本不起作用”是什么意思。在httpclient方法中,我将这些数据发送到将文本转换为语音的服务器,作为回报,我使用该转换文本的音频文件。在httpurlconnection方法中,数据正在发送,但我收到的返回的音频文件是空的,我猜发送的数据是不对的。有什么建议吗?