0

我已经搜索了很多,但对 android 没有任何好处..我希望有人可以帮助我。我想获取此 url 上的文件列表并将它们添加到列表视图中的字符串中..所以我的问题是我无法从 url 获取文件列表..这是我的 url.. 文件

我搜索并找到了这个..但它适用于java而不是android..有没有办法转换它?

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Sample {
    public static void main(String[] args) throws IOException {
        Document doc = Jsoup.connect("http://howto.unixdev.net").get();
        for (Element file : doc.select("td.right td a")) {
            System.out.println(file.attr("href"));
        }
    }
}

我还发现服务器必须具有目录列表能力..对吗?我如何找到服务器具有这种能力?我找到了这个,但需要转换 :( 我正在处理它 :D 在这里

我测试了这个方法,但我得到了错误..你能帮我找出我的错误吗?

  URL url1;
            List serverDir;

            try {
                  url1 = new URL("http://sourceforge.net/projects/ganjoor/files/gdb/");    
                InputStreamReader urls=new InputStreamReader(url1.openStream(), "UTF-8");
                Log.i("url_string", urls.toString());
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

这是原木猫

04-11 08:20:22.772: E/AndroidRuntime(13416): FATAL EXCEPTION: main
04-11 08:20:22.772: E/AndroidRuntime(13416): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.tosca.persianpoem/co.tosca.persianpoem.Download_database}: java.lang.NullPointerException
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.os.Looper.loop(Looper.java:137)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.ActivityThread.main(ActivityThread.java:4441)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at dalvik.system.NativeStart.main(Native Method)
04-11 08:20:22.772: E/AndroidRuntime(13416): Caused by: java.lang.NullPointerException
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpConnection$Address.hashCode(HttpConnection.java:343)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at java.util.HashMap.get(HashMap.java:298)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:67)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at java.net.URL.openStream(URL.java:462)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at co.tosca.persianpoem.Download_database.onCreate(Download_database.java:45)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.Activity.performCreate(Activity.java:4465)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-11 08:20:22.772: E/AndroidRuntime(13416):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-11 08:20:22.772: E/AndroidRuntime(13416):    ... 11 more

我检查了许可证并添加了这两个许可证,但仍然出现错误..我的错误在哪里?

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" /> 

现在我尝试了这个新代码,但我又得到了错误。有人可以帮我解决这个新错误吗?

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


        String contents=getContents("http://sourceforge.net/projects/ganjoor/files/gdb/");
        Log.i("download", contents);
    }
    else{
        Toast.makeText(this, "you are not connected to internet..Please check your connections", 1).show();
    }
}




          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();
          }
public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

这是错误

   04-11 22:13:01.560: E/AndroidRuntime(18988): FATAL EXCEPTION: main
04-11 22:13:01.560: E/AndroidRuntime(18988): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.tosca.persianpoem/co.tosca.persianpoem.Download_database}: android.os.NetworkOnMainThreadException
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.os.Looper.loop(Looper.java:137)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.ActivityThread.main(ActivityThread.java:4441)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at java.lang.reflect.Method.invoke(Method.java:511)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at dalvik.system.NativeStart.main(Native Method)
04-11 22:13:01.560: E/AndroidRuntime(18988): Caused by: android.os.NetworkOnMainThreadException
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at co.tosca.persianpoem.Download_database.getContents(Download_database.java:62)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at co.tosca.persianpoem.Download_database.onCreate(Download_database.java:45)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.Activity.performCreate(Activity.java:4465)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-11 22:13:01.560: E/AndroidRuntime(18988):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-11 22:13:01.560: E/AndroidRuntime(18988):    ... 11 more
4

1 回答 1

1

它接缝你在android上使用它。确保您获得使用互联网访问权限!将此添加到您的清单中:

好的,您在这里得到的是当您尝试连接到主线程中的网络时抛出的“NetworkOnMainThreadException”。为什么抛出它?:网络连接可能需要很长时间才能得到答案(想想你想在慢速互联网连接上下载一个大文件)。如果您在主要活动中开始此下载 - 活动将被阻止,直到下载完成。这意味着,用户不能触摸或使用任何东西。因此,这在 Android 中是被禁止的。

您必须在另一个线程中进行 URL 连接!我推荐http://developer.android.com/reference/android/os/AsyncTask.html impementation。使用 asynctask,您可以使结果在 GUI 中可见。

于 2013-04-11T10:41:04.807 回答