0
이 비동기 작업의 내 코드 갤러리파일을 업로드하는 동안 비동기 작업에서 오류가 발생했습니다.
private class uploadFileAsync extends AsyncTask<String[],Void, Void>{
ProgressDialog dialog = new ProgressDialog(CrimeReportActivity.this);
@Override
protected void onPreExecute() {
// update the UI immediately after the task is executed
super.onPreExecute();
dialog.setMessage("Please wait...");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(String[]... params) {
String fileUploadResp= fs.fileUpload(filePath, uploadURL);
System.out.println(fileUploadResp);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
에서 이미지를 따기 서버 에 이미지를 업로드하는이 내 업로드 클래스입니다
public class FileShare {
@SuppressWarnings("deprecation")
public String fileUpload(String videoPath, String uploadURL) {
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 10 * 1024 * 1024;
try {
// ------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(
videoPath));
// open a URL connection to the Servlet
URL url = new URL(uploadURL);
// 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=\""
+ videoPath + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
Log.e("Debug", "File is written");
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());
while ((str = inStream.readLine()) != null) {
Log.e("Debug", "Server Response " + str);
}
inStream.close();
} catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
return str;
}
이것은 로그 캣
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298)
at com.pstpl.crimeverify.CrimeReportActivity$uploadFileAsync.doInBackground(CrimeReportActivity.java:376)
at com.pstpl.crimeverify.CrimeReportActivity$uploadFileAsync.doInBackground(CrimeReportActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
입니다
doinbackground에 serverresponse 문자열을 가져 오려고합니다. 나는 무엇이 잘못 되었습니까?
doInBackground 메서드는 onPostExecuteMethod 값을 반환해야합니다. 나는 그것이 틀릴지도 모른다 것을 나는 짐작한다. 그것은 단지 추측입니다. doInBackGround()의 반환 유형을 String으로 설정하고 메서드에서 "1"을 반환합니다. 맞춰보세요. – Beginner
예외적으로 String fileUploadResp = fs.fileUpload (filePath, uploadURL; null 객체를 얻고있는 것 같습니다.) – Robin
그렇지만 null이되는 이유는 무엇입니까? – WISHY