【问题标题】:Get Website text from URL into String (Android)将网站文本从 URL 获取为字符串(Android)
【发布时间】: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 输出更新您的问题。

标签: java android


【解决方案1】:

在获得 HTTPResponse 之前,您的代码看起来不错,底部(对字符串的响应)可以进行很多优化。值得注意的是Android won't let you to do network operation on the main thread,所以请考虑使用AsyncTask 来执行您的http GET 操作。

public String getURLtext(String zielurl) throws IllegalStateException, IOException

{
    String result = ""; // default empty string
    try
    {
        String meineurl = zielurl;

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(meineurl);
    HttpResponse response = httpClient.execute(httpGet, localContext);

    InputStream is = response.getEntity().getContent();
    result = inputStreamToString(is).toString();
    }
    catch (Exception ex)
    {
       // do some Log.e here
    }
    finally
    {
        return result;          
    }
}

// Fast Implementation
private StringBuilder inputStreamToString(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();

    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    // Read response until the end
    try {
        while ((line = rd.readLine()) != null) { 
            total.append(line); 
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Return full string
    return total;
}

【讨论】:

  • 感谢您的快速答复!我复制了代码并将其粘贴到我的 Android Eclipse 项目中并尝试再次对其进行测试。这次没有错误,但是当我单击按钮时,文本字段保持空白,没有任何反应。
  • 如果添加 ex.printStackTrace();在 catch 块中?您是否按照我的建议尝试了 AsyncTask?
猜你喜欢
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 2012-02-11
相关资源
最近更新 更多