weixin_42218038
随缘56
采纳率18.2%
2019-05-06 17:42

okhttp抛出, Response response = client.newCall(request).execute();报错,该怎么解决

5
 try {
               MediaType JSON = MediaType.parse("application/json; charset=utf-8");
               OkHttpClient client = new OkHttpClient();
               RequestBody body = RequestBody.create(JSON,ObjToJson.tojson(user));
               Request request = new Request.Builder()
                        .addHeader("Connection","close")
                        .url(Location.login)
                        .post(body)
                        .build();
                Log.v("url",Location.login);
                Log.v("body",body.toString());
                Response response = client.newCall(request).execute();
                if (response.isSuccessful()) {
                    User user2= JsonToObj.toUser(response.body().toString());
                    Toast.makeText(LoginActivity.this,user2.getTel(),Toast.LENGTH_SHORT).show();
                }else{
                    new Exception("Unexpected code " + response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
V/url: http://120.79.146.32:8080/EasyToCreat2.0/User/login.action
V/body: okhttp3.RequestBody$2@90bbbec
W/System.err: android.os.NetworkOnMainThreadException
W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:355)
W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
        at java.net.Socket.connect(Socket.java:616)
        at okhttp3.internal.Platform$Android.connectSocket(Platform.java:211)
        at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:140)
        at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
W/System.err:     at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188)
        at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127)
        at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
        at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289)
        at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241)
        at okhttp3.RealCall.getResponse(RealCall.java:240)
        at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
        at okhttp3.RealCall.execute(RealCall.java:57)
W/System.err:     at com.example.easytocreate.LoginActivity$button_login_loginClick.onClick(LoginActivity.java:60)
        at android.view.View.performClick(View.java:6256)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • onceing Angus博客 2年前

    W/System.err: android.os.NetworkOnMainThreadException 这句话的意思就是在主线程,,new一个子线程就解决了。。。

    点赞 1 评论 复制链接分享
  • weixin_45023698 好好学习天天向夏 2年前

    Android使用OkHttpClient请求网络资源,不能放到主线程上执行,可以new一个新线程执行网络请求

    点赞 1 评论 复制链接分享
  • qq_37280924 张学徒 5天前

    添加新线程,`Response response = client.newCall(request).execute();` 这一行改为下面这样

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Response response = okHttpClient.newCall(request).execute();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
    点赞 评论 复制链接分享

相关推荐