我是android和java的新手,即时尝试使用此字符串
String textSource = "https://sites.google.com/site/androidersite/text.txt";
但是我的应用停止了工作。如果我从字符串中删除“//”工作正常。 我怎么能在我的字符串中使用“//”? 感谢
这是我的代码:
public void Conectar(String miStr){
String textResult;
String textSource = "https://sites.google.com/site/androidersite/text.txt";
Toast.makeText(c, "test: " + textSource, Toast.LENGTH_LONG).show();
URL textUrl;
try {
textUrl = new URL("https://"+textSource);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(textUrl.openStream()));
String stringBuffer;
String stringText = "";
while((stringBuffer = bufferReader.readLine()) != null) {
stringText += stringBuffer;
}
bufferReader.close();
textResult = stringText;
Toast.makeText(c, "ojala: " + textResult, Toast.LENGTH_LONG).show();
} catch(MalformedURLException e) {
e.printStackTrace();
textResult = e.toString();
} catch(IOException e) {
e.printStackTrace();
textResult = e.toString();
}
//return null;
}
问题是String我不知道为什么textSource中的“//”让我的应用程序崩溃
我使用
尝试答案textUrl = new URL("https://sites.google.com/site/androidersite/text.txt");
String textSource = textUrl.toString();
但仍然崩溃
这是我的logcat
11-10 16:30:10.707 11694-11694/? I/art﹕ Late-enabling -Xcheck:jni
11-10 16:30:11.056 11694-11715/com.example.testusr.calculadora1 D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-10 16:30:11.063 11694-11694/com.example.testusr.calculadora1 D/Atlas﹕ Validating map...
11-10 16:30:11.155 11694-11715/com.example.testusr.calculadora1 I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020_msm8960_LA.AF.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 03/30/15 Mon
Local Branch: mybranch8688311
Remote Branch: quic/LA.AF.1.1_rb1.16
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020 + 9b2699f + 2215637 + 60aa592 + f2362e6 + 5c64f59 + 82411a1 + 1f36e07 + NOTHING
11-10 16:30:11.156 11694-11715/com.example.testusr.calculadora1 I/OpenGLRenderer﹕ Initialized EGL, version 1.4
11-10 16:30:11.184 11694-11715/com.example.testusr.calculadora1 D/OpenGLRenderer﹕ Enabling debug mode 0
11-10 16:30:17.291 11694-11694/com.example.testusr.calculadora1 D/AndroidRuntime﹕ Shutting down VM
11-10 16:30:17.294 11694-11694/com.example.testusr.calculadora1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testusr.calculadora1, PID: 11694
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1161)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:199)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
at java.net.URL.openStream(URL.java:470)
at com.example.testusr.calculadora1.Conexion.Conectar(Conexion.java:48)
at com.example.testusr.calculadora1.MainActivity.btnBuscarClic(MainActivity.java:84)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19884)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
11-10 16:30:18.690 11694-11694/com.example.testusr.calculadora1 I/Process﹕ Sending signal. PID: 11694 SIG: 9
答案 0 :(得分:0)
为什么不让textSource
成为URL
?然后当你想要它是一个字符串时:
URL textSource = new URL("https://sites.google.com/site/androidersite/text.txt");
String urlStr = textSource.toString(); // or use .toString() wherever you need it to be a string