我希望把我的上传图片功能放在 AsyncTask 中。因此我改变了,它仍然有错误。现在,我认为我的问题在于 onPostExecute。httpURLConnection 操作需要放在 onPostExecute 吗?我应该改变什么?谢谢你对我的帮助
class upload extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Saving product
* */
protected String doInBackground(String... args) {
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(actionUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// setting the memory
httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
// allow input and output
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// use POST way
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ srcPath.substring(srcPath.lastIndexOf("/") + 1)
+ "\""
+ end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(srcPath);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
while ((count = fis.read(buffer)) != -1)
{
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
dos.close();
is.close();
return result;
} catch (Exception e)
{
e.printStackTrace();
setTitle(e.getMessage());
}
}
return null;
}
protected void onPostExecute(String file_url) {
Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show();
// dismiss the dialog once product uupdated
pDialog.dismiss();
}
}
LOGCAT
04-22 09:01:53.381: E/AndroidRuntime(1154): FATAL EXCEPTION: AsyncTask #1
04-22 09:01:53.381: E/AndroidRuntime(1154): java.lang.RuntimeException: An error occured while executing doInBackground()
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.lang.Thread.run(Thread.java:1096)
04-22 09:01:53.381: E/AndroidRuntime(1154): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.view.ViewRoot.invalidateChild(ViewRoot.java:607)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:633)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2505)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.view.View.invalidate(View.java:5139)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.widget.TextView.checkForRelayout(TextView.java:5364)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.widget.TextView.setText(TextView.java:2688)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.widget.TextView.setText(TextView.java:2556)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.widget.TextView.setText(TextView.java:2531)
04-22 09:01:53.381: E/AndroidRuntime(1154): at com.android.internal.policy.impl.PhoneWindow.setTitle(PhoneWindow.java:260)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.app.Activity.onTitleChanged(Activity.java:3581)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.app.Activity.setTitle(Activity.java:3547)
04-22 09:01:53.381: E/AndroidRuntime(1154): at com.example.mmap.MainActivity$upload.doInBackground(MainActivity.java:255)
04-22 09:01:53.381: E/AndroidRuntime(1154): at com.example.mmap.MainActivity$upload.doInBackground(MainActivity.java:1)
04-22 09:01:53.381: E/AndroidRuntime(1154): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-22 09:01:53.381: E/AndroidRuntime(1154): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-22 09:01:53.381: E/AndroidRuntime(1154): ... 4 more
04-22 09:01:54.411: E/WindowManager(1154): Activity com.example.mmap.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46047368 that was originally added here
04-22 09:01:54.411: E/WindowManager(1154): android.view.WindowLeaked: Activity com.example.mmap.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@46047368 that was originally added here
04-22 09:01:54.411: E/WindowManager(1154): at android.view.ViewRoot.<init>(ViewRoot.java:247)
04-22 09:01:54.411: E/WindowManager(1154): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-22 09:01:54.411: E/WindowManager(1154): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-22 09:01:54.411: E/WindowManager(1154): at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-22 09:01:54.411: E/WindowManager(1154): at android.app.Dialog.show(Dialog.java:241)
04-22 09:01:54.411: E/WindowManager(1154): at com.example.mmap.MainActivity$upload.onPreExecute(MainActivity.java:187)
04-22 09:01:54.411: E/WindowManager(1154): at android.os.AsyncTask.execute(AsyncTask.java:391)
04-22 09:01:54.411: E/WindowManager(1154): at com.example.mmap.MainActivity$1.onClick(MainActivity.java:68)
04-22 09:01:54.411: E/WindowManager(1154): at android.view.View.performClick(View.java:2408)
04-22 09:01:54.411: E/WindowManager(1154): at android.view.View$PerformClick.run(View.java:8816)
04-22 09:01:54.411: E/WindowManager(1154): at android.os.Handler.handleCallback(Handler.java:587)
04-22 09:01:54.411: E/WindowManager(1154): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 09:01:54.411: E/WindowManager(1154): at android.os.Looper.loop(Looper.java:123)
04-22 09:01:54.411: E/WindowManager(1154): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 09:01:54.411: E/WindowManager(1154): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 09:01:54.411: E/WindowManager(1154): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 09:01:54.411: E/WindowManager(1154): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 09:01:54.411: E/WindowManager(1154): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 09:01:54.411: E/WindowManager(1154): at dalvik.system.NativeStart.main(Native Method)