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();
}
}
어디에서 코드를 사용 했습니까? – Biu
죄송합니다, onCreate 메서드입니다. 이에 따라 업데이 트하십시오. (! 페치) {'무한 루프가있는 동안 당신이 당신의 로그 캣을 확인 했 – Brian
이 '(이 불가피한 NPE에 의해 파괴되기 때문에 실제로는하지 않습니다)? – njzk2