从android模拟器调用localhost

时间:2016-06-22 09:05:52

标签: java android android-studio localhost

抱歉,因为它已经问了很多次,但实际上我找不到答案,我正在运行localhost:8080的本地计算机上运行一个Web服务,我想从运行的应用程序中调用该服务Android模拟器,但它一直崩溃与此错误消息(不幸的是应用已停止)。

这是我的代码

public void HttpRequest(View view)throws Exception{
    BufferedReader reader = null;
    StringBuilder stringBuilder;
    URL url = new URL("http://10.0.2.2:8080");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    stringBuilder = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null)
    {
        stringBuilder.append(line + "\n");
    }
}

这是LogCat

06-22 08:57:11.733 2370-2370/com.example.ssalameh.myfirstapp I/art: Not late-enabling -Xcheck:jni (already on)
    06-22 08:57:11.785 2370-2370/com.example.ssalameh.myfirstapp W/System: ClassLoader referenced unknown path: /data/app/com.example.ssalameh.myfirstapp-3/lib/x86
    06-22 08:57:14.699 2370-2370/com.example.ssalameh.myfirstapp W/System: ClassLoader referenced unknown path: /data/app/com.example.ssalameh.myfirstapp-3/lib/x86
    06-22 08:57:15.294 2370-2380/com.example.ssalameh.myfirstapp W/art: Suspending all threads took: 42.728ms
    06-22 08:57:15.458 2370-2370/com.example.ssalameh.myfirstapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    06-22 08:57:15.721 2370-2376/com.example.ssalameh.myfirstapp W/art: Suspending all threads took: 49.366ms
    06-22 08:57:15.909 2370-2526/com.example.ssalameh.myfirstapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

[ 06-22 08:57:15.912  2370: 2370 D/         ]
HostConnection::get() New Host Connection established 0xaff08410, tid 2370
06-22 08:57:15.985 2370-2526/com.example.ssalameh.myfirstapp I/OpenGLRenderer: Initialized EGL, version 1.4
06-22 08:57:16.320 2370-2370/com.example.ssalameh.myfirstapp D/item: Food
06-22 08:57:17.597 2370-2370/com.example.ssalameh.myfirstapp D/AndroidRuntime: Shutting down VM
06-22 08:57:17.619 2370-2370/com.example.ssalameh.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ssalameh.myfirstapp, PID: 2370
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
at java.net.Socket.connect(Socket.java:884)
at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
at com.android.okhttp.Connection.connect(Connection.java:152)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
at com.example.ssalameh.myfirstapp.MainActivity.HttpRequest(MainActivity.java:55)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

任何人都可以帮忙吗?谢谢你。

0 个答案:

没有答案