我试图从我的应用程序上传图像到php服务器。 上传过程工作正常,但上传后“不幸的应用程序关闭”我不明白问题出在哪里。AsyncTask中的致命异常不幸的应用程序关闭
从我的片段类我打电话im_up.execute(path)
,我实现了一个名为AsyncResponse
接口具有processFinish(String output)
方法从的AsyncTask数据得到我的片段类
public class UploadImage extends AsyncTask<Void, Void, String> {
int serverResponseCode = 0;
ProgressDialog dialog = null;
public AsyncResponse delegate=null;
String path;
public UploadImage(String pathUri,Context mcontext) {
// TODO Auto-generated constructor stub
path=pathUri;
ctx=mcontext;
dialog = ProgressDialog.show(ctx,"Loading","Plaease Wait",true);
}
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
String upLoadServerUri = "http://www.pinnacle2k14.com/letsmeet/upload.php";
String fileName =path;
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
File sourceFile = new File(path);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File Does not exist");
return "0";
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
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("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
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);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("uploadFile", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
if(serverResponseCode == 200){
Log.i("response", "File Upload complete");
//Toast.makeText(getActivity(), "File Upload Complete.", Toast.LENGTH_SHORT).show();
}
//close the streams //
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
dialog.dismiss();
ex.printStackTrace();
Log.i("response", "MalformedURLException");
//Toast.makeText(getActivity(), "MalformedURLException", Toast.LENGTH_SHORT).show();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
dialog.dismiss();
e.printStackTrace();
Log.i("response", e.toString());
//Toast.makeText(getActivity(), "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e);
}
dialog.dismiss();
//return serverResponseCode;
return "hello";
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
delegate.processFinish(result);
}
}
在片段
UploadImage up_im=new UploadImage(getRealPathFromURI(path),getActivity());
up_im.delegate=this;
up_im.execute();
在UploadImage.java什么是线109? –
@ManishDubey dialog.dismiss();在返回“你好”之前; – DharanBro