【发布时间】:2014-02-03 23:00:09
【问题描述】:
首先我必须说我是 Android 编程的初学者,在一般编程方面完全没有经验。但现在我决定制作一个小应用程序供我私人使用。
在我的应用程序中,我需要将给定 URL 中的一些文本转换为字符串。我在网上找到了一些方法并对其进行了一些个性化。但是出现了一些问题,因为当我使用 Android Eclipse 模拟器运行该应用程序时,它显示“不幸的是,xxx_app 已停止。”。
这是我的代码:
public String getURLtext(String zielurl) throws IllegalStateException, IOException{
String meineurl = zielurl;
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(meineurl);
HttpResponse response = httpClient.execute(httpGet, localContext);
String result = "";
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
}
return result;
}
这是我想在 EditText text1 中显示输出字符串的方法。
public void test(View view) throws InterruptedException, ExecutionException, IllegalStateException, IOException {
EditText text1 = (EditText)findViewById(R.id.textfeld);
String teststring = getURLtext("http://ephemeraltech.com/demo/android_tutorial20.php");
text1.setText(teststring);
}
如果有人可以帮助我,我会很高兴。
谢谢!
【问题讨论】:
-
当您遇到错误时,请始终查看应该显示原因的 logcat。如果看不到该窗口,请在 Eclipse 中使用 Window -> ShowView -> Logcat。正如已经回答的那样,一个问题是主线程上的网络。解决了这个问题后,如果您还有其他错误,请使用 logcat 输出更新您的问题。