[英]How to send an InputStream using POST request
I want to send a mp3 file using POST request from android studio to a flask server. 我想使用POST请求从android studio向flask服务器发送mp3文件。 I have read an mp3 file from device and stored in input stream object.
我已经从设备读取了mp3文件,并将其存储在输入流对象中。 The flask server identifies the request to be a POST request and creates the mp3 file.
flask服务器将请求标识为POST请求并创建mp3文件。 But when I play the mp3 file it does not work.
但是,当我播放mp3文件时,它不起作用。 So the connection part does not have any problem.
因此,连接部分没有任何问题。 I would like to know how to send this inputstream object in my POST request and get it successfully play the mp3 file on the server (Raspberry Pi).
我想知道如何在POST请求中发送此inputstream对象,并使其成功播放服务器(Raspberry Pi)上的mp3文件。
The part to get the mp3 file and store in inputstream: 获取mp3文件并将其存储在inputstream中的部分:
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();
}
The part of sending the POST request: 发送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) {
}
}
I solved the problem. 我解决了问题。 Below is the solution.
下面是解决方案。
The part to get the mp3 file and store in inputstream: 获取mp3文件并将其存储在inputstream中的部分:
//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();
}
The part of sending the POST request: 发送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) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.