从url中读取文件java到android问题 [英] Read file from url java to android problems
问题描述
我在我的大学提供的在线存储上有一个txt文件,我想在android应用程序中读取它。我已经设法在Java中这样做,但是当我在Android中尝试这样做时,什么都不会发生。 Eclipse的建议改变了代码,没有任何工作。请帮助我我是新的android开发。
a.txt文件有内容:这是一条消息!
这是java代码: / p>
import java.io. *;
导入java.net。*;
public class Java_Parser {
public static void main(String [] args)throws Exception {
$ b $ URL URL = new URL(http: //cgi.di.uoa.gr/~std10108/a.txt);
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine; ((inputLine = in.readLine())!= null)
System.out.println(inputLine);
in.close();
输出结果如下:This是一个消息!
我已经将互联网权限添加到清单,所以它不是问题。
我复制粘贴到一个Android项目的功能,并在Eclipse的建议后,它转向这一点:$ b
$ b pre $ //这是一个单击按钮时调用的函数
public void OnClick(View view){
URL oracle = null;
BufferedReader in = null;
String inputLine;
String text = null;
尝试{
oracle = new URL(http://cgi.di.uoa.gr/~std10108/a.txt);
} catch(MalformedURLException e){
// TODO自动生成的catch块
e.printStackTrace();
尝试{
in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace(); ((inputLine = in.readLine())!= null)
text = text + inputLine;
尝试{
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
尝试{
in.close();
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
EditText edittext =(EditText)findViewById(R.id.editText1);
edittext.setText(text);
$ b 应用程序编译成功,但是当我尝试运行它时点击按钮,它崩溃。你可以帮我吗?
这是logcat
> 11-20 17:40:04.089:I /进程(9926):发送信号。 PID:9926 SIG:9
> 11-20 17:50:37.009:D / libEGL(10534):加载
> /system/lib/egl/libEGL_mali.so 11-20 17:50:37.014:D / libEGL(10534):
> loaded /system/lib/egl/libGLESv1_CM_mali.so 11-20 17:50:37.014:
> D / libEGL(10534):loaded /system/lib/egl/libGLESv2_mali.so 11-20
> 17:50:37.024:D /(10534):设备驱动程序API匹配11-20 17:50:37.024:
> D /(10534):设备驱动程序API版本:10 11-20 17:50:37.024:
> D /(10534):用户空间API版本:10 11-20 17:50:37.024:D /(10534):
> mali:REVISION = Linux-r2p4-02rel0 BUILD_DATE =星期二Oct 16 15:37:13 KST
> D / OpenGLRenderer(10534):启用调试模式
> 2012 11-20 17:50:37.054: 0 11-20 17:50:39.449:D / AndroidRuntime(10534):关闭虚拟机11-20
> 17:50:39.449:W / dalvikvm(10534):threadid = 1:线程退出
>未捕获的异常(group = 0x416d22a0)11-20 17:50:39.454:
> E / AndroidRuntime(10534):致命例外:main 11-20 17:50:39.454:
> E / AndroidRuntime(10534):java.lang.IllegalStateException:Could not
>执行活动的方法11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> android.view.View $ 1.onClick(View.java:3704)11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> android.view.View.performClick(View.java:4232)11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> android.view.View $ PerformClick.run(View.java:17318)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> android.os.Handler.handleCallback(Handler.java:615)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> android.os.Handler.dispatchMessage(Handler.java:92)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> android.os.Looper.loop(Looper.java:137)11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> android.app.ActivityThread.main(ActivityThread.java:4921)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> java.lang.reflect.Method.invokeNative(Native Method)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> java.lang.reflect.Method.invoke(Method.java:511)11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> dalvik.system.NativeStart.main(Native Method)11-20 17:50:39.454:
> E / AndroidRuntime(10534):引起:
> java.lang.reflect.InvocationTargetException 11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> java.lang.reflect.Method.invokeNative(Native Method)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> java.lang.reflect.Method.invoke(Method.java:511)11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> android.view.View $ 1.onClick(View.java:3699)11-20 17:50:39.454:
> E / AndroidRuntime(10534):... 11更多11-20 17:50:39.454:
> E / AndroidRuntime(10534):引起:
> android.os.NetworkOnMainThreadException 11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> java.net.InetAddress.lookupHostByName(InetAddress.java:385)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> java.net.InetAddress.getAllByName(InetAddress.java:214)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpConnection。< init>(HttpConnection.java:70)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpConnection。< init>(HttpConnection.java:50)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpConnection.connect(HttpConnection.java:128)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpEngine.connect(HttpEngine.java:310)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)11-20
> 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
> 11-20 17:50:39.454:E / AndroidRuntime(10534):at
> java.net.URL.openStream(URL.java:462)11-20 17:50:39.454:
> E / AndroidRuntime(10534):at
> com.example.test.MainActivity.OnClick(MainActivity.java:55)11-20
> 17:50:39.454:E / AndroidRuntime(10534):... 14 more
Edit1:I添加互联网权限,但没有任何改变
解决方案在onCreate方法中添加以下代码:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build();
StrictMode.setThreadPolicy(policy);
添加到ManiFestFile:
< uses-permission android:name =android.permission.INTERNET/>
I have a txt file at the online storage that my university offers me and I want to read it at an android application. I've managed to do this in Java but when I try to do this in Android nothing happens. The Eclipse suggestions changes the code and nothing works. Please help me I am new to android development.
The a.txt file has content: This is a message!
This is the java code:
import java.io.*;
import java.net.*;
public class Java_Parser {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://cgi.di.uoa.gr/~std10108/a.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
And the output is, as it should be : This is a message!
I have added the internet permission to the manifest so it's not the problem.
I copy paste it in a function on an android project and after the suggestions of Eclipse it turns to this:
//This a function called when a button is clicked
public void OnClick(View view){
URL oracle = null;
BufferedReader in = null;
String inputLine;
String text=null;
try {
oracle = new URL("http://cgi.di.uoa.gr/~std10108/a.txt");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while ((inputLine = in.readLine()) != null)
text=text + inputLine;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EditText edittext= (EditText) findViewById(R.id.editText1);
edittext.setText(text);
}
The application compiles succesfully but when I try to run it and click the button, it crashes. Can you help me?
This is the logcat
> 11-20 17:40:04.089: I/Process(9926): Sending signal. PID: 9926 SIG: 9
> 11-20 17:50:37.009: D/libEGL(10534): loaded
> /system/lib/egl/libEGL_mali.so 11-20 17:50:37.014: D/libEGL(10534):
> loaded /system/lib/egl/libGLESv1_CM_mali.so 11-20 17:50:37.014:
> D/libEGL(10534): loaded /system/lib/egl/libGLESv2_mali.so 11-20
> 17:50:37.024: D/(10534): Device driver API match 11-20 17:50:37.024:
> D/(10534): Device driver API version: 10 11-20 17:50:37.024:
> D/(10534): User space API version: 10 11-20 17:50:37.024: D/(10534):
> mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST
> 2012 11-20 17:50:37.054: D/OpenGLRenderer(10534): Enabling debug mode
> 0 11-20 17:50:39.449: D/AndroidRuntime(10534): Shutting down VM 11-20
> 17:50:39.449: W/dalvikvm(10534): threadid=1: thread exiting with
> uncaught exception (group=0x416d22a0) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): FATAL EXCEPTION: main 11-20 17:50:39.454:
> E/AndroidRuntime(10534): java.lang.IllegalStateException: Could not
> execute method of the activity 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> android.view.View$1.onClick(View.java:3704) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> android.view.View.performClick(View.java:4232) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> android.view.View$PerformClick.run(View.java:17318) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> android.os.Handler.handleCallback(Handler.java:615) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> android.os.Handler.dispatchMessage(Handler.java:92) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> android.os.Looper.loop(Looper.java:137) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> android.app.ActivityThread.main(ActivityThread.java:4921) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> java.lang.reflect.Method.invokeNative(Native Method) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> java.lang.reflect.Method.invoke(Method.java:511) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> dalvik.system.NativeStart.main(Native Method) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): Caused by:
> java.lang.reflect.InvocationTargetException 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> java.lang.reflect.Method.invokeNative(Native Method) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> java.lang.reflect.Method.invoke(Method.java:511) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> android.view.View$1.onClick(View.java:3699) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): ... 11 more 11-20 17:50:39.454:
> E/AndroidRuntime(10534): Caused by:
> android.os.NetworkOnMainThreadException 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> java.net.InetAddress.lookupHostByName(InetAddress.java:385) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> java.net.InetAddress.getAllByName(InetAddress.java:214) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpConnection.<init>(HttpConnection.java:70) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpConnection.<init>(HttpConnection.java:50) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpEngine.connect(HttpEngine.java:310) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
> 11-20 17:50:39.454: E/AndroidRuntime(10534): at
> java.net.URL.openStream(URL.java:462) 11-20 17:50:39.454:
> E/AndroidRuntime(10534): at
> com.example.test.MainActivity.OnClick(MainActivity.java:55) 11-20
> 17:50:39.454: E/AndroidRuntime(10534): ... 14 more
Edit1: I added the Internet permission but nothing changed
解决方案 Add this code in the onCreate method :-
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ADD this to ManiFestFile:
<uses-permission android:name="android.permission.INTERNET"/>
这篇关于从url中读取文件java到android问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!