0

我正在做android平臺的解密任務。起初,我創建了一個名爲RunDecrypt的方法。當我按下我的UI中的按鈕時,它工作正常。下面所示的方法:Android異步任務運行時異常錯誤。

public void runDecrypt() throws IOException{ 
    EditText editText = (EditText) findViewById(R.id.fileName); 
    EditText editText2 = (EditText) findViewById(R.id.keyName); 

    String fileName = editText.getText().toString(); 
    String keyName = editText2.getText().toString(); 

    File sdcard = Environment.getExternalStorageDirectory(); 
    File file = new File(sdcard , fileName); 
    File key = new File(sdcard, keyName); 


    BufferedReader brFile; 
    String Cipher = null; 
    try{ 
     //Read file line by line and concat each line of string in file with space character. 
     FileInputStream fstream = new FileInputStream(file); 
     brFile = new BufferedReader(new InputStreamReader(fstream)); 

     String tempString; 

     while((tempString = brFile.readLine()) != null){ 

      if(Cipher == null){ 
       Cipher = tempString; 

      }else{ 
       Cipher = Cipher.concat(" "); 
       Cipher = Cipher.concat(tempString); 

      } 

     } 

    }catch(Exception e){ 
     //messageBox("Decrypt", e.getMessage()); 
    } 

    BufferedReader brKey; 
    String DecKey = null; 

    try{ 
     FileInputStream fstream = new FileInputStream(key); 
     brKey = new BufferedReader(new InputStreamReader(fstream)); 
     DecKey = brKey.readLine(); 

    }catch(Exception e){ 
     //messageBox("Decrypt", e.getMessage()); 
    } 


    try{ 
     byte[] cipherByte = DES.parseBytes(Cipher); 
     String decKey = DES.convertStringToHex(DecKey); 

     byte[] keyByte = DES.parseBytes(decKey); 
     String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte)); 

     String temp = decryptResult.replace(" ", ""); 
     String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount())); 
     String finResult = DES.convertHexToString(finalDecrypt); 

     TextView FinalResult = (TextView)findViewById(R.id.decryptText); 
     FinalResult.setText(finResult); 

    }catch(Exception e){ 
     messageBox("Decrypt", "Please Upload File Properly"); 
    } 
} 

由於它的做工精細,我嘗試實施這種方法與異步任務運行我的工作背景。

private class runDecrypt extends AsyncTask <URL , Integer, Long> { 
    private final ProgressDialog dialog = new ProgressDialog(Homepage.this); 
    AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this); 
    @SuppressWarnings("resource") 
    @Override 
    protected Long doInBackground(URL... params) { 
     EditText editText = (EditText) findViewById(R.id.fileName); 
     EditText editText2 = (EditText) findViewById(R.id.keyName); 

     String fileName = editText.getText().toString(); 
     String keyName = editText2.getText().toString(); 

     File sdcard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdcard , fileName); 
     File key = new File(sdcard, keyName); 


     BufferedReader brFile; 
     String Cipher = null; 
     try{ 
      //Read file line by line and concat each line of string in file with space character. 
      FileInputStream fstream = new FileInputStream(file); 
      brFile = new BufferedReader(new InputStreamReader(fstream)); 

      String tempString; 

      try { 
       while((tempString = brFile.readLine()) != null){ 

        if(Cipher == null){ 
         Cipher = tempString; 

        }else{ 
         Cipher = Cipher.concat(" "); 
         Cipher = Cipher.concat(tempString); 

        } 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     }catch(java.io.FileNotFoundException e){ 
      System.out.println("File could not be found."); 
     } 

     BufferedReader brKey; 
     String DecKey = null; 
     try{ 
      FileInputStream fstream = new FileInputStream(key); 
      brKey = new BufferedReader(new InputStreamReader(fstream)); 
      try { 
       DecKey = brKey.readLine(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     }catch(java.io.FileNotFoundException e){ 
      System.out.println("Key file could not be found."); 
     } 

     String decKey = DES.convertStringToHex(DecKey);  
     byte[] cipherByte = DES.parseBytes(Cipher); 
     byte[] keyByte = DES.parseBytes(decKey); 
     String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte)); 

     String temp = decryptResult.replace(" ", ""); 
     String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount())); 
     String finResult = DES.convertHexToString(finalDecrypt); 

     TextView FinalResult = (TextView)findViewById(R.id.decryptText); 
     FinalResult.setText(finResult); 
     return null; 
    } 
    protected void onPreExecute() { 
     this.dialog.setMessage("Decrypting..."); 
     this.dialog.show(); 
     } 

    protected void onPostExecute(Long result) { 

     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 
     } 
     builder.setMessage("Decryption Completed"); 
     builder.show(); 

    } 
    protected void onProgressUpdate(int progress) { 
     setProgress(progress * 100); 
    } 
} 

我的onClick方法:即實現的顯示在下面的類

public void onClick (View v){ 
    Intent browse = new Intent(this, Browse.class); 
    switch (v.getId()){ 
    case R.id.browseFile: 
     browse.putExtra("browse","file"); 
     startActivityForResult(browse, 1); 
     break; 
    case R.id.browseKey: 
     browse.putExtra("browse", "key"); 
     startActivityForResult(browse, 1); 
     break; 
    case R.id.decrypt: 
     new runDecrypt().execute(); 
     break; 

    default: 
     break; 
    } 
} 

我的logcat如下圖所示: logcat

任何人都可以請幫助?謝謝你,感謝!

+1

'doInBackground()'在後臺線程上被調用。從UI讀取應該在UI線程調用的'onPreExecute()'方法內完成。 –

回答

-1

由於Shashank kadhe提到在onPostExecute方法中嘗試這一點,請根據您自己的需要進行更改。

protected void onPostExecute(String file_url) { 

String fileName = editText.getText().toString(); 
     String keyName = editText2.getText().toString(); 

     File sdcard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdcard , fileName); 
     File key = new File(sdcard, keyName); 

BufferedReader brFile; 
    String Cipher = null; 
    try{ 
     //Read file line by line and concat each line of string in file with space character. 
     FileInputStream fstream = new FileInputStream(file); 
     brFile = new BufferedReader(new InputStreamReader(fstream)); 

     String tempString; 

     while((tempString = brFile.readLine()) != null){ 

      if(Cipher == null){ 
       Cipher = tempString; 

      }else{ 
       Cipher = Cipher.concat(" "); 
       Cipher = Cipher.concat(tempString); 

      } 

     } 

    }catch(Exception e){ 
     //messageBox("Decrypt", e.getMessage()); 
    } 

    BufferedReader brKey; 
    String DecKey = null; 

    try{ 
     FileInputStream fstream = new FileInputStream(key); 
     brKey = new BufferedReader(new InputStreamReader(fstream)); 
     DecKey = brKey.readLine(); 

    }catch(Exception e){ 
     //messageBox("Decrypt", e.getMessage()); 
    } 


    try{ 
     byte[] cipherByte = DES.parseBytes(Cipher); 
     String decKey = DES.convertStringToHex(DecKey); 

     byte[] keyByte = DES.parseBytes(decKey); 
     String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte)); 

     String temp = decryptResult.replace(" ", ""); 
     String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount())); 
     String finResult = DES.convertHexToString(finalDecrypt); 

     TextView FinalResult = (TextView)findViewById(R.id.decryptText); 
     FinalResult.setText(finResult); 

    }catch(Exception e){ 
     messageBox("Decrypt", "Please Upload File Properly"); 
    } 
} 

Since it's work fine, I try to implement this method with Async Task for running my work in background. The class that implemented shown below: 

private class runDecrypt extends AsyncTask <URL , Integer, Long> { 
    private final ProgressDialog dialog = new ProgressDialog(Homepage.this); 
    AlertDialog.Builder builder = new AlertDialog.Builder(Homepage.this); 
    @SuppressWarnings("resource") 
    @Override 
    protected Long doInBackground(URL... params) { 
     EditText editText = (EditText) findViewById(R.id.fileName); 
     EditText editText2 = (EditText) findViewById(R.id.keyName); 

     String fileName = editText.getText().toString(); 
     String keyName = editText2.getText().toString(); 

     File sdcard = Environment.getExternalStorageDirectory(); 
     File file = new File(sdcard , fileName); 
     File key = new File(sdcard, keyName); 


     BufferedReader brFile; 
     String Cipher = null; 
     try{ 
      //Read file line by line and concat each line of string in file with space character. 
      FileInputStream fstream = new FileInputStream(file); 
      brFile = new BufferedReader(new InputStreamReader(fstream)); 

      String tempString; 

      try { 
       while((tempString = brFile.readLine()) != null){ 

        if(Cipher == null){ 
         Cipher = tempString; 

        }else{ 
         Cipher = Cipher.concat(" "); 
         Cipher = Cipher.concat(tempString); 

        } 

       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     }catch(java.io.FileNotFoundException e){ 
      System.out.println("File could not be found."); 
     } 

     BufferedReader brKey; 
     String DecKey = null; 
     try{ 
      FileInputStream fstream = new FileInputStream(key); 
      brKey = new BufferedReader(new InputStreamReader(fstream)); 
      try { 
       DecKey = brKey.readLine(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     }catch(java.io.FileNotFoundException e){ 
      System.out.println("Key file could not be found."); 
     } 

     String decKey = DES.convertStringToHex(DecKey);  
     byte[] cipherByte = DES.parseBytes(Cipher); 
     byte[] keyByte = DES.parseBytes(decKey); 
     String decryptResult = DES.hex(DES.decryptCBC(cipherByte, keyByte)); 

     String temp = decryptResult.replace(" ", ""); 
     String finalDecrypt = temp.substring(0, (temp.length() - DES.getConcatCount())); 
     String finResult = DES.convertHexToString(finalDecrypt); 

     TextView FinalResult = (TextView)findViewById(R.id.decryptText); 
     FinalResult.setText(finResult); 
} 
} 
+0

我明白你的意思了,謝謝! –

0

有你的代碼的各種問題,但特別異常的原因在logcat的解釋正確的有:

只有創建視圖層次可以觸摸其觀點原來的線程。

有問題的行是com.example.descracker.Homepage:245,你從一個的AsyncTask一個TextView調用setText()。您必須將此邏輯移入UI線程,例如通過AsyncTask的設施功能onPreExecute()onPostExecute()onProgressUpdate(),或者使用post(Runnable)向視圖本身發佈延遲操作。

此外,請遵循Java編碼約定,並以小寫字母開始變量名稱。

+0

非常感謝!這是幫助! –