使用JsonReader.setLenient(true)在第1行第1列路径$处接受格式错误的JSON [英] Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

查看:8060
本文介绍了使用JsonReader.setLenient(true)在第1行第1列路径$处接受格式错误的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的片段:

  public class news extends Fragment {


private RecyclerView recyclerView;
private ArrayList< Deatails>数据;
私人DataAdapter适配器;
private查看myFragmentView;



@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){

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



$ b 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();

$ b $ private void loadJSON(){
if(isNetworkConnected()){

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(拦截器)
.retryOnConnectionFailure(true)
.connectTimeout(15,TimeUnit.SECONDS)
.build ();

Gson gson = new GsonBuilder()
.setLenient()
.create();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(http://www.memaraneha.ir/)
.client(客户端)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

RequestInterface request = retrofit.create(RequestInterface.class);
呼叫< JSONResponse> call = request.getJSON();
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.show();
call.enqueue(new Callback< JSONResponse>(){
@Override
public void onResponse(Call< JSONResponse> call,Response< JSONResponse> response){
progressDialog.dismiss ();
JSONResponse jsonResponse = response.body();
data.addAll(Arrays.asList(jsonResponse.getAndroid()));
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call< JSONResponse> call,Throwable t){
progressDialog.dismiss();
Log.d(Error,t.getMessage ());
}
});
}
else {
Toast.makeText(getActivity()。getApplicationContext(),دستگاهشمابهاینترنتمتصلنیست!,Toast.LENGTH_LONG).show();}

private boolean isNetworkConnected(){
ConnectivityManager cm =(ConnectivityManager)getActivity()。getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if(ni == null){
//没有活动的网络。
返回false;
} else
返回true;


$ / code $ / pre
$ b

RequestInterface

  public interface RequestInterface {

@GET(Erfan / news.php)
Call< JSONResponse>的getJSON();
}

解决方案

这是已知问题,并基于 setLenient



<$ p $ Gson gson = new GsonBuilder()
.setLenient()
.create();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(客户端)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

如果您将此添加到改造中,现在会出现另一个错误:

  com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_OBJECT,但在第1行第1列为STRING $ 

这也是一个众所周知的错误,你可以找到答案这里(这意味着您的服务器响应格式不正确) ;因此,改变服务器响应返回以下内容:

  {
android:[
{ver:1.5 ,名称:Cupcace,api:Api Level 3}
...
]
}

为了更好地理解,请将您的回答与 Github api 进行比较。



建议:找出 request / response add HttpLoggingInterceptor 在您的改造中。

基于回答您的 ServiceHelper 为:

  private ServiceHelper(){
httpClient = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.interceptors()。add(interceptor);
Retrofit retrofit = createAdapter()。build();
service = retrofit.create(IService.class);
}

另外,请不要忘记添加:

  compile'c​​om.squareup.okhttp3:logging-interceptor:3.3.1'


What is this error ? How can I fix this? My app is running but can't load data. And this is my Error: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

This is my fragment :

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();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.memaraneha.ir/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        final ProgressDialog progressDialog = new ProgressDialog(getActivity());
        progressDialog.show();
        call.enqueue(new Callback<JSONResponse>() {
            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                progressDialog.dismiss();
                JSONResponse jsonResponse = response.body();
                data.addAll(Arrays.asList(jsonResponse.getAndroid()));
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                progressDialog.dismiss();
                Log.d("Error", t.getMessage());
            }
        });
    }
    else {
        Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show();}
}
private boolean isNetworkConnected() {
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni == null) {
        // There are no active networks.
        return false;
    } else
        return true;
}
}

RequestInterface :

public interface RequestInterface {

@GET("Erfan/news.php")
Call<JSONResponse> getJSON();
}

解决方案

This is known issue and based on this answer you should add setLenient:

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

If you add this to your retrofit Now it's gives you another error:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

This is also a well-known error you can find answer here (this is means that your server response is not well-formatted); So change server response to return following:

{
    android:[
        { ver:"1.5", name:"Cupcace", api:"Api Level 3" }
        ...
    ]
}

For better comprehension compare your response with Github api.

Suggestion: to find out what's going on to your request/response add HttpLoggingInterceptor in your retrofit.

Based on this answer your ServiceHelper would be:

private ServiceHelper() {
        httpClient = new OkHttpClient.Builder();
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.interceptors().add(interceptor);
        Retrofit retrofit = createAdapter().build();
        service = retrofit.create(IService.class);
    }

Also don't forget to add:

compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'

这篇关于使用JsonReader.setLenient(true)在第1行第1列路径$处接受格式错误的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆