我想使用来自android studio的POST请求向烧瓶服务器发送一个mp3文件。我从设备读取了一个mp3文件并存储在输入流对象中。烧瓶服务器将请求标识为POST请求并创建mp3文件。但是当我播放mp3文件时它不起作用。所以连接部分没有任何问题。我想知道如何在我的POST请求中发送这个输入流对象并让它成功播放服务器上的mp3文件(Raspberry Pi)。
获取mp3文件并存储在输入流中的部分:
if(!DocumentsContract.isDocumentUri(this, data.getData()))
throw new RuntimeException("Not a documentsContract document");
try {
InputStream is = getContentResolver().openInputStream(data.getData());
new Main4Activity.httpAsyncTask417().execute();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
发送POST请求的部分:
public class httpAsyncTask417 extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... strings) {
try {
String url="http://172.17.57.132/post_songs";
URL obj=new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
String urlParameters = "content=";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
Log.v("HTTPDelete_Check3", "Get returned: " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
}catch(java.io.IOException ex) {
}
return null;
}
@Override
protected void onPostExecute(Void v) {
}
}
答案 0 :(得分:0)
我解决了这个问题。以下是解决方案。
获取mp3文件并存储在输入流中的部分:
//if(!DocumentsContract.isDocumentUri(this, data.getData()))
// throw new RuntimeException("Not a documentsContract document");
try {
is = getContentResolver().openInputStream(data.getData());
uri=data.getData();
file_name=getFileName(uri);
new Main4Activity.httpAsyncTask417().execute();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
发送POST请求的部分:
try {
String url="http://172.17.59.97/post_songs?title="+file_name;
URL obj=new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
/*BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = r.readLine()) != null) {
wr.writeBytes(line+'\n');
}*/
//wr.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + uri.toString() +"\"" + "\r\n");
byte []buffer = new byte[4096];
int read = 0;
while ( (read = is.read(buffer)) != -1 ) {
wr.write(buffer, 0, read);
}
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
Log.v("HTTPDelete_Check3", "Get returned: " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
}catch(java.io.IOException ex) {
}