2015-01-16 1 views
0
try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet(wyvernBones); //"http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones" 
     HttpResponse response = httpClient.execute(httpGet, localContext); 
     String result = ""; 
     BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     fetched = false; 
     progBox.show(); 
     while (!fetched) { 
      line = br.readLine(); 
      if (line.contains("Average Sell Offer")) { 
       Toast.makeText(this, line, Toast.LENGTH_LONG).show(); 
       progBox.dismiss(); 
       fetched = true; 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

의 내부 코드를 실행하지 않습니다.안드로이드 앱은 위 내가 사용하는 첫 번째 방법은 try 블록

public void getItem() throws Exception 
{ 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpContext localContext = new BasicHttpContext(); 
    HttpGet httpGet = new HttpGet(wyvernBones); //"http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones" 
    HttpResponse response = httpClient.execute(httpGet, localContext); 
    String result = ""; 
    BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
    fetched = false; 
    progBox.show(); 
    while (!fetched) { 
     line = br.readLine(); 
     if (line.contains("Average Sell Offer")) { 
      Toast.makeText(this, line, Toast.LENGTH_LONG).show(); 
      progBox.dismiss(); 
      fetched = true; 
     } 
    } 
} 

다음 코드를 사용하지 않고 try catch 블록에서 사용한 후 대신 메소드에 넣습니다. 디버깅하는 동안 try/catch 블록 내의 코드가 처리/실행 (?)조차되지 않았다는 것을 깨달았습니다. 나는 무엇을 잘못 했는가?

예 : 첫 번째 샘플에서 onCreate이 방법을 시도하고, 버튼을 누를 때, 제 2 샘플 불렸다.

전체 코드;

public class MainActivity extends ActionBarActivity implements OnClickListener{ 

Button btn1; 

ProgressDialog progBox; 
Boolean fetched; 
String line; 

URL url; 

String wyvernBones = "http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    progBox = new ProgressDialog(this); 
    progBox.setIndeterminate(true); 
    progBox.setTitle("Fetching Data.."); 

    btn1 = (Button) findViewById(R.id.btn1); 
    btn1.setOnClickListener(this); 

    try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet(wyvernBones); //"http://forums.zybez.net/runescape-2007-prices/3104-wyvern-bones" 
     HttpResponse response = httpClient.execute(httpGet, localContext); 
     String result = ""; 
     BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
     fetched = false; 
     progBox.show(); 
     while (!fetched) { 
      line = br.readLine(); 
      if (line.contains("Average Sell Offer")) { 
       Toast.makeText(this, line, Toast.LENGTH_LONG).show(); 
       progBox.dismiss(); 
       fetched = true; 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 
+0

어디에서 코드를 사용 했습니까? – Biu

+0

죄송합니다, onCreate 메서드입니다. 이에 따라 업데이 트하십시오. (! 페치) {'무한 루프가있는 동안 당신이 당신의 로그 캣을 확인 했 – Brian

+3

이 '(이 불가피한 NPE에 의해 파괴되기 때문에 실제로는하지 않습니다)? – njzk2

답변

1

의견은 "문제점"에 대해 언급하지 않고 언급했습니다. UI 스레드에서 httpClient.execute을 수행 할 수 없습니다. onCreate이 실행됩니다.

android httpclient.execute exception

당신이 코드가 실행되지 않지만, 그렇게 일을 할 나타납니다 생각하는 이유는 catch 당신이 그 예외입니다. 당신의 로그 캣을 확인한 다음 AsyncTask

편집에 읽어 : 당신이 멀티 스레딩 및 장점/함정을 잘 알고있는 경우

당신은 또한 백그라운드 스레드를 사용할 수 있습니다. AsyncTask은 자신의 장점/함정을 가지고 있기 때문에 실제로 내가 보통 일을하는 방법입니다.

+0

팁 주셔서 감사. Thread (Runnable)에서이 코드를 실행할 수 있습니까? – Brian

+1

물론입니다! 나는 그것을 반영하기 위해 내 대답을 수정했습니다. – Jim

+0

내 문제가 해결되었습니다! 고마워요 !!! – Brian

관련 문제