私はたくさん検索しましたが、Androidに適したものは何もありません..誰かが私を助けてくれることを願っています.このURLでファイルのリストを取得し、それらを文字列に追加してリストビューに表示したい.. URLからファイルのリストを取得できない..ここに私のURLがあります.. ファイル
私はこれを検索して見つけました..しかし、Android用ではなくJava用です..変換する方法はありますか?
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 here
この方法をテストしましたが、エラーが発生しました.間違いを見つけるのを手伝ってもらえますか?
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
パーミッションを確認し、この 2 つのパーミッションを追加しましたが、それでもエラーが発生します..どこが間違っていますか?
<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