Android: How to send song file to server throught HttpPost along with other variables

只谈情不闲聊 提交于 2019-12-06 07:53:48

问题


I am to send a song file to server through HttpPost. Currently I am using this code to send data to server

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());
        }

But to send song file to server I have find this code on net but having problems to integrate these both.

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
}

Please help me so that I could integrate these both or some other solution, so that I could send Music file to server along with other authentication data.

I am sending data to server through secure restful service.

thanks.

来源:https://stackoverflow.com/questions/11598457/android-how-to-send-song-file-to-server-throught-httppost-along-with-other-vari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!