0

i hope to put my upload image function in AsyncTask. therefore i changes, it still had error. now,i think that my problem in onPostExecute. Do httpURLConnection operation need to put in onPostExecute? what should i change? thank you for your help me

 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) 
7
  • show your error, that will be easy to solve your problem Commented Apr 22, 2013 at 9:17
  • What does this function do setTitle(e.getMessage());. Update ui ?? Commented Apr 22, 2013 at 9:18
  • Are you getting result in onPostExecute()?? Commented Apr 22, 2013 at 9:18
  • i have to upload the logCAT Commented Apr 22, 2013 at 9:24
  • @HimYikTang setTitle(e.getMessage()); does the function update ui? It seems you are updating ui from the background thread. You need to update ui on the Ui thread. Do it in onPostExecute() Commented Apr 22, 2013 at 9:29

2 Answers 2

2

Remove the return result, because you cannot return from child view.
remove

setTitle(e.getMessage());

from doinbackground And rewrite postexecute like this

protected void onPostExecute(String file_url) { // dismiss the dialog once product uupdated pDialog.dismiss(); Toast.makeText(MainActivity.this, file_url, Toast.LENGTH_LONG).show(); } 
Sign up to request clarification or add additional context in comments.

10 Comments

i remove the return result also the problem still observe
Remove setTitle(e.getMessage()); also
and remove unnecessary bracket after doninBackground
THANK YOU i have success to upload image,but the toast never show any test.
write toast after dialog.dismiss() and toast the result. you can store result in variable in doinbackground method and show it in onPostexecte method
|
0

It looks like this method:

setTitle(e.getMessage()); 

Is accessing UI Thread from outside of it. All that touchech UI must be done outside of doInBackground() method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.