gpt4 book ai didi

java - 不幸的是,AsyncTask 中的致命异常应用程序关闭

转载 作者:行者123 更新时间:2023-11-29 21:12:23 25 4
gpt4 key购买 nike

我正在尝试将图像从我的应用程序上传到 PHP 服务器。上传过程工作正常,但在上传“不幸的是应用程序关闭”后,我不明白问题出在哪里。

从我的 fragment 类中调用 im_up.execute(path) 并且我实现了一个名为 AsyncResponse 的接口(interface),它有一个 processFinish(String output) 从 AsyncTask 获取数据到我的 fragment 类的方法

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);

}

}

在 fragment 中

UploadImage up_im=new UploadImage(getRealPathFromURI(path),getActivity());
up_im.delegate=this;
up_im.execute();

最佳答案

问题是 dialog.dismiss() 行。您不能从并行线程触摸 android UI。它应该只在 UI 线程中完成,即主线程。

“不要从 UI 线程外部访问 Android UI 工具包”

将该行移动到 onPostExecute() 或从那里删除它。

关于java - 不幸的是,AsyncTask 中的致命异常应用程序关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22398686/

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