【问题标题】:how to download website content (text) to String in Android Studio如何在 Android Studio 中将网站内容(文本)下载到 String
【发布时间】:2015-09-23 12:20:05
【问题描述】:

我是 Android 开发的新手,并且已经启动了第一个真正的应用程序,它需要从网站下载内容并将其转换为字符串(可以是源代码或其他东西,我会削减我需要的内容)。我已经搜索了 3 天的 stackoverflow 和整个互联网,但我的低技能并没有给我工作应用程序,总是出现一些错误,缺课等, 这个链接对我没有帮助: Get text from web page to string Android: get HTML from web page as String with HttpClient not working loading data from site as String(Android) Downloading a website to a string How do you Programmatically Download a Webpage in Java

---编辑

这是我现在正在工作的源代码,基于http://www.coderzheaven.com/2011/07/17/how-to-read-webpage-contents-as-a-string-in-android/

    package tm.tresura;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtils {

  public static String getContents(String url) {
        String contents ="";

  try {
        URLConnection conn = new URL(url).openConnection();

        InputStream in = conn.getInputStream();
        contents = convertStreamToString(in);
   } catch (MalformedURLException e) {
        Log.v("MALFORMED URL EXCEPTION");
   } catch (IOException e) {
        Log.e(e.getMessage(), e);
   }

  return contents;
}

private static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {

      BufferedReader reader = new BufferedReader(new    
                              InputStreamReader(is, "UTF-8"));
        StringBuilder sb = new StringBuilder();
         String line = null;
         try {
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "n");
                }
           } catch (IOException e) {
                e.printStackTrace();
           } finally {
                try {
                        is.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
            }
        return sb.toString();
  }
}

和错误:

Error:(76, 16) error: no suitable method found for v(String)
method Log.v(String,String,Throwable) is not applicable
(actual and formal argument lists differ in length)
method Log.v(String,String) is not applicable
(actual and formal argument lists differ in length)

Error:(78, 16) error: no suitable method found for e(String,IOException)
method Log.e(String,String,Throwable) is not applicable
(actual and formal argument lists differ in length)
method Log.e(String,String) is not applicable
(actual argument IOException cannot be converted to String by method invocation conversion)

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

【问题讨论】:

标签: android string tostring


【解决方案1】:
Log.v("MALFORMED URL EXCEPTION");

afaik v() 有不同的签名,此方法需要两个参数 - 标记和一些正文,尝试添加额外的 String 参数

Log.v("TAG","MALFORMED URL EXCEPTION");

【讨论】:

    【解决方案2】:

    谢谢你的帮助,也许这对你来说很明显,但对我来说不是,我缺乏知识。现在编译器工作正常,但应用程序“意外退出”。我正在做这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多