0

나는 안드로이드 플랫폼에서 암호 해독 작업을하고 있습니다. 처음에는 RunDecrypt라는 메서드를 만듭니다. 내 UI에서 버튼을 누르면 잘 작동합니다. 아래에 표시된 방법은 다음과 같습니다.Android 비동기 작업 런타임 예외 오류.

잘 작동하므로 백그라운드에서 작업을 실행하기위한 비동기 작업으로이 메서드를 구현하려고합니다.

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

내 온 클릭 방법 : 아래와 같이 구현 된 클래스

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

는 누구나 도와 주시겠습니까? 감사합니다.

+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

은 당신의 코드에 여러 가지 문제가 있지만 특정 예외의 원인은 로그 캣에서 바로 설명 : 견해를 만질 수있는 뷰 계층 구조를 만들어

만 원래 스레드.

해당 라인은 com.example.descracker.Homepage:245이며, 여기서 AsyncTask의 TextView에 setText()을 호출합니다. 이 논리를 AsyncTask의 기능 함수 onPreExecute(), onPostExecute() 또는 onProgressUpdate()을 통해 또는 post(Runnable)을 사용하여 지연된 작업을보기 자체에 게시하여이 논리를 UI 스레드로 옮겨야합니다.

또한 Java 코딩 규칙을 따르고 변수 이름을 소문자로 시작하십시오.

+0

정말 고마워!, 도움이 됐어! –