gpt4 book ai didi

Android- POST 多部分表单数据

转载 作者:IT老高 更新时间:2023-10-28 23:30:29 26 4
gpt4 key购买 nike

我想发送包含视频和数据(例如电子邮件和姓名)的多部分表单。以下是我的代码,它不起作用,服务器没有响应

File file =new File(VideoPath);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Name", "LDGHT"));
nameValuePairs.add(new BasicNameValuePair("Email", "hhh@example.com"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setEntity(new FileEntity(file, "video/mp4"));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

最佳答案

查看我的帖子以发送多部分数据 Post Data to Server with Multipart ..

还可以查看此以获取引用 Link

new Task_finder().execute();

上传视频文件到服务器:

    public class Task_finder extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Facebook_Post_View.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileName = file_path;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "YOUR PHP LINK FOR UPLOADING IMAGE";
try{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
dos = new DataOutputStream( conn.getOutputStream() );
dos.writeBytes(twoHyphens + boundary + lineEnd);

dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + upload_file_name + "\"" + lineEnd); // uploaded_file_name is the Name of the File to be uploaded
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0){
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex){
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe){
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null){
Log.e("Debug","Server Response "+str);
reponse_data=str;
}
inStream.close();
}
catch (IOException ioex){
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
return null;
}

@Override
protected void onPostExecute(Void result) {

if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}

关于Android- POST 多部分表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7645284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com