Android 如何在doInBackground中发送变量和上传视频?[安卓]
在上传视频之前,我试图将变量发送到服务器上的PHP脚本。我在DoInBackground方法的其他try-catch块中使用此代码将变量发送到不同的PHP脚本:Android 如何在doInBackground中发送变量和上传视频?[安卓],android,Android,在上传视频之前,我试图将变量发送到服务器上的PHP脚本。我在DoInBackground方法的其他try-catch块中使用此代码将变量发送到不同的PHP脚本: URL url = new URL(UploadVideo_URL);//Create a new URL and put there variable "register_URL" into it. HttpURLConnection httpURLConnection = (HttpURLConnec
URL url = new URL(UploadVideo_URL);//Create a new URL and put there variable "register_URL" into it.
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//Create a httpConnection and open it
httpURLConnection.setRequestMethod("POST");//Use the request method
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("ProductAccountName", "UTF-8") + "=" + URLEncoder.encode(ProductAccountName, "UTF-8") + "&" +
URLEncoder.encode("fileName", "UTF-8") + "=" + URLEncoder.encode(fileName, "UTF-8") + "&" +
URLEncoder.encode("ProductName", "UTF-8") + "=" + URLEncoder.encode(ProductName, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
我想我可以用这个try-catch块(上传视频)做同样的事情,但是当我尝试在代码中使用它时,会出现一个错误
try{
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(UploadVideo_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("myFile", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
OutputStream OS = conn.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
String data = URLEncoder.encode("ProductAccountName", "UTF-8") + "=" + URLEncoder.encode(ProductAccountName, "UTF-8") + "&" +
URLEncoder.encode("fileName", "UTF-8") + "=" + URLEncoder.encode(fileName, "UTF-8") + "&" +
URLEncoder.encode("ProductName", "UTF-8") + "=" + URLEncoder.encode(ProductName, "UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
bytesAvailable = fileInputStream.available();
Log.i("Huzza", "Initial .available : " + bytesAvailable);
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);
serverResponseCode = conn.getResponseCode();
fileInputStream.close();
dos.flush();
dos.close();
return "video uploaded";
} catch (Exception e) {
e.printStackTrace();
}
我的问题是:
如何发送变量,然后在同一个Try-catch块中上载视频
错误如下:
03-15 12:33:50.044 21790-21790/com.wilsapp.wilsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.wilsapp.wilsapp, PID: 21790
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.wilsapp.wilsapp.BackgroundTask.onPostExecute(BackgroundTask.java:660)
at com.wilsapp.wilsapp.BackgroundTask.onPostExecute(BackgroundTask.java:27)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我想它出错的原因是因为我把代码放错地方了
重要的是,我发送变量,然后在同一个try-catch块中上传视频!提前感谢请粘贴完整的doInBackground()代码,因为崩溃日志描述了您尚未粘贴的行。请粘贴完整的doInBackground()代码,因为崩溃日志描述了您尚未粘贴的行。