2014-03-14 36 views
1

我试图从我的应用程序上传图像到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(); 
+0

在UploadImage.java什么是线109? –

+0

@ManishDubey dialog.dismiss();在返回“你好”之前; – DharanBro

回答

1

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

移到该行onPostExecute()“不从UI线程外部访问Android的UI工具包”或从那里删除它。

+0

没有被overrided方法命名为'onPostUpdate()',它应该是'onPostExecute()'。 –

+0

@ManishDubey ..对不起,这是错字。纠正它。 – Sushil

1

你应该关闭onPostExecute()方法中的对话框。

dialog.dismiss(); 


@Override 
protected void onPostExecute(String result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    dialog.dismiss(); 
    delegate.processFinish(result); 
} 

而且你刚才宣布的变量

ProgressDialog dialog = null; 

我看不出它已经被初始化。因此,通过

dialog = ProgressDialog.show(getActivity(),"Loading","Plaease Wait",true); 

UPDATE初始化:

使全局变量

Context ctx; 

现在从

public UploadImage(String pathUri) { 
    // TODO Auto-generated constructor stub 
    path=pathUri; 
    } 

改变你的构造函数

public UploadImage(Context mContext ,String pathUri) { 
    // TODO Auto-generated constructor stub 
    ctx = mContext; 
    path = pathUri; 

    } 

现在这个CTX变量传递给你的ProgressDialog现场,这样将改变

dialog = ProgressDialog.show(ctx,"Loading","Plaease Wait",true); 
+0

因为它是一个AsyncTask我不能让我的活动在那里..但我删除它,它的工作正常......如何提及我的活动呢? – DharanBro

+0

再次检查我的答案。 – Piyush

+0

我试过了,我无法在那里调用getActivity – DharanBro