2014-04-07 5 views
0

나는 안드로이드를 처음 접했다. 내가 뭘하고 싶은지 그냥 Asynctask에 URL을 전달하고 반환 상태 코드 SC_OK 여부를 확인합니다. 이 메소드는 상태 코드 SC_OK가 반환되면 true를 반환하고 다른 상태 코드가 있으면 false를 반환해야합니다.안드로이드에서 Asynctask에서 부울 값을 반환

class checking extends AsyncTask<String, Void, Boolean> { 

    @Override 
    protected Boolean doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     Boolean a; 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(l1);  
     HttpResponse response; 
     try { 
      response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode != HttpStatus.SC_OK) {         
       Toast.makeText(getApplicationContext(),"No", Toast.LENGTH_LONG).show(); 
       a= false; 
      } 
      else { 
       Toast.makeText(getApplicationContext(),"Yes", Toast.LENGTH_LONG).show(); 
       a= true; 
      } 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally{ 
      return false; 
     }    
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 

    } 
} 
+0

그리고 뭐가 문제입니까? – Math

+0

'finally' 블록이 항상 호출되므로 항상 false를 반환합니다. 어쩌면이 블록을 제거하고 return a를 추가하려고합니다. 결국? –

+0

작동하지 않았지만 제대로 작동합니다. – AisneT

답변

1

변경 (블라인드 코딩)에 모든 것을 :

class checking extends AsyncTask<String, Void, Boolean> { 
    @Override 
    protected Boolean doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     Boolean a; 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(l1); 
     HttpResponse response; 
     try { 
      response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 
      if (statusCode != HttpStatus.SC_OK) { 
       a = false; 
      } else { 
       a = true; 
      } 
      return a; 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return false; 
    } 
    @Override 
    protected void onPostExecute(Boolean result) { 
     Toast.makeText(getApplicationContext(), (result ? "Yes" : "No"), Toast.LENGTH_LONG).show(); 
    } 
} 
+0

정말 고마워요. 그게 효과가 있었고, 내가 틀린 곳이 있어요. – AisneT

+0

환영합니다 ... – gunar

1

당신은 true에 해당 지역의 부울 a를 설정하지만 결코 값을 반환하지 있습니다. 이 메소드는 작성된대로 항상 false을 리턴합니다.

관련 문제