使用JsonReader.setLenient(true)在第1行第1列路径$接受格式错误的JSON

这是什么错误?我怎样才能解决这个问题?我的应用程序正在运行,但无法加载数据。这是我的错误:使用JsonReader.setLenient(true)在第1行第1列路径$接受格式错误的JSON


这是我的片段:


public class news extends Fragment {



private RecyclerView recyclerView;

private ArrayList<Deatails> data;

private DataAdapter adapter;

private View myFragmentView;




@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    myFragmentView = inflater.inflate(R.layout.news, container, false);

    initViews();

    return myFragmentView;


}



private void initViews() {

    recyclerView = (RecyclerView) myFragmentView.findViewById(R.id.card_recycler_view);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());

    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(layoutManager);

    data = new ArrayList<Deatails>();

    adapter = new DataAdapter(getActivity(), data);

    recyclerView.setAdapter(adapter);


    new Thread()

    {

        public void run()

        {

            getActivity().runOnUiThread(new Runnable() {

                @Override

                public void run() {

                    loadJSON();

                }

            });


        }

    }

    .start();

}


private void loadJSON() {

    if (isNetworkConnected()){


        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder()

                .addInterceptor(interceptor)

                .retryOnConnectionFailure(true)

                .connectTimeout(15, TimeUnit.SECONDS)

                .build();


        Gson gson = new GsonBuilder()

                .setLenient()

                .create();


总是这个错误与您的json无关,可能是来自错误的请求,为了更好地处理,首先请在邮递员中检查您的请求(如果您得到了响应),然后将您的json响应与您的模型进行比较(如果没有错误的话),则该错误来自错误的请求,当您的响应未启动时也可能发生(在某些情况下响应可能是html)


慕后森
浏览 2011回答 3
3回答

江户川乱折腾

当响应内容类型不是时,也会发生此问题application/json。在我的情况下,响应内容类型为text/html,我遇到了这个问题。我将其更改为application/json工作。
打开App,查看更多内容
随时随地看视频慕课网APP