我要通过 HttpPost 将歌曲文件发送到服务器。目前我正在使用此代码将数据发送到服务器
HttpPost postRequest = new HttpPost();
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", Splash.pref.getString("userEmail", "")));
nameValuePairs.add(new BasicNameValuePair("password", Splash.pref.getString("userPassword", "")));
nameValuePairs.add(new BasicNameValuePair("name", etName.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("title", ttfSongTitle.getText().toString()));
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// construct a URI objectedsta
postRequest.setURI(new URI(serviceURL));
} catch (URISyntaxException e) {
Log.e("URISyntaxException", e.toString());
}
但是要将歌曲文件发送到服务器,我在网上找到了这段代码,但是在整合这两者时遇到了问题。
String url = "http://yourserver";
File file = new File(Environment.getExternalStorageDirectory(),
"yourfile");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}
请帮助我,以便我可以集成这两者或其他一些解决方案,以便我可以将音乐文件与其他身份验证数据一起发送到服务器。
我正在通过安全的休息服务将数据发送到服务器。
谢谢。