这个Log.d有什么问题

时间:2017-05-06 06:38:25

标签: android logcat okhttp android-logcat

如果我对它进行评论,那么Log.d()有什么问题,然后没有错误显示,但是显示了Log.d()值,但也得到了FATAL EXCEPTION:AsyncTask#1和app崩溃。

包com.sha.okhttp;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new AsyncTask<Void,Void,String>() {

            @Override
            protected String doInBackground(Void... params) {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://api.github.com/users/mdshareque")
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    Log.d(TAG, "doInBackground() called with: " + "params = [" + response.body().string() + "]");
                    return response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }
        }.execute();


    }
}

日志

05-06 11:53:47.346 16677-16677/? I/art: Not late-enabling -Xcheck:jni (already on)
05-06 11:53:47.346 16677-16677/? W/art: Unexpected CPU variant for X86 using defaults: x86
05-06 11:53:47.366 16677-16684/? E/art: Failed sending reply to debugger: Broken pipe
05-06 11:53:47.368 16677-16684/? I/art: Debugger is no longer active
05-06 11:53:47.368 16677-16684/? I/art: Starting a blocking GC Instrumentation
05-06 11:53:47.472 16677-16677/? W/System: ClassLoader referenced unknown path: /data/app/com.sha.okhttp-1/lib/x86
05-06 11:53:47.478 16677-16677/? I/InstantRun: starting instant run server: is main process
05-06 11:53:47.479 16677-16677/? V/InstantRun: Starting server socket listening for package com.sha.okhttp on android.net.LocalSocketAddress@2b08a24
05-06 11:53:47.480 16677-16677/? V/InstantRun: Started server for package com.sha.okhttp
05-06 11:53:47.551 16677-16677/? 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
05-06 11:53:47.761 16677-16701/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-06 11:53:47.819 16677-16702/? I/OpenGLRenderer: Initialized EGL, version 1.4
05-06 11:53:47.819 16677-16702/? D/OpenGLRenderer: Swap behavior 1
05-06 11:53:47.858 16677-16677/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-06 11:53:49.398 16677-16701/com.sha.okhttp D/MainActivity: doInBackground() called with: params = [{"login":"mdshareque","id":17742511,"avatar_url":"https://avatars1.githubusercontent.com/u/17742511?v=3","gravatar_id":"","url":"https://api.github.com/users/mdshareque","html_url":"https://github.com/mdshareque","followers_url":"https://api.github.com/users/mdshareque/followers","following_url":"https://api.github.com/users/mdshareque/following{/other_user}","gists_url":"https://api.github.com/users/mdshareque/gists{/gist_id}","starred_url":"https://api.github.com/users/mdshareque/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdshareque/subscriptions","organizations_url":"https://api.github.com/users/mdshareque/orgs","repos_url":"https://api.github.com/users/mdshareque/repos","events_url":"https://api.github.com/users/mdshareque/events{/privacy}","received_events_url":"https://api.github.com/users/mdshareque/received_events","type":"User","site_admin":false,"name":"Shareque","company":null,"blog":"","location":null,"email":null,"hireable":true,"bio":null,"public_repos":3,"public_gists":0,"followers":0,"following":1,"created_at":"2016-03-09T11:18:48Z","updated_at":"2017-05-03T05:01:40Z"}]

                                                              --------- beginning of crash
05-06 11:53:49.400 16677-16701/com.sha.okhttp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                Process: com.sha.okhttp, PID: 16677
                                                                java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                    at android.os.AsyncTask$3.done(AsyncTask.java:325)
                                                                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                    at java.lang.Thread.run(Thread.java:761)
                                                                 Caused by: java.lang.IllegalStateException: closed
                                                                    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:397)
                                                                    at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:391)
                                                                    at okhttp3.internal.Util.bomAwareCharset(Util.java:431)
                                                                    at okhttp3.ResponseBody.string(ResponseBody.java:173)
                                                                    at com.sha.okhttp.MainActivity$1.doInBackground(MainActivity.java:36)
                                                                    at com.sha.okhttp.MainActivity$1.doInBackground(MainActivity.java:23)
                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:305)
                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                    at java.lang.Thread.run(Thread.java:761) 

有什么遗漏

1 个答案:

答案 0 :(得分:4)

String s  = response.body().string();
Log.d(TAG,"Response: "+s);
return s

把它放在你的try catch块中。这应该可以解决你的崩溃。您正在呼叫 response.body()。字符串 两次。应该只调用一次。