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

erf*_*fan 82 android json gson retrofit

这个错误是什么?我怎样才能解决这个问题?我的应用正在运行,但无法加载数据.这是我的错误:使用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();

        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(), "Internet is disconnected", 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;
}
}
Run Code Online (Sandbox Code Playgroud)

RequestInterface:

public interface RequestInterface {

@GET("Erfan/news.php")
Call<JSONResponse> getJSON();
}
Run Code Online (Sandbox Code Playgroud)

一个

UPDATE

总是这个错误不是关于你的json,它可能来自你的错误请求,为了更好的处理首先检查你的请求邮递员,如果你有响应然后比较你的json响应与你的模型,如果没有错,那么这个错误来自你的错误请求,当你的回答不是josn(在某些情况下响应可能是html)时也会发生这种情况

Ami*_*mir 141

这是一个众所周知的问题,根据这个答案,您可以添加setLenient:

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

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();
Run Code Online (Sandbox Code Playgroud)

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

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
Run Code Online (Sandbox Code Playgroud)

这是另一个众所周知的错误,你可以在这里找到答案(这个错误意味着你的服务器响应格式不正确); 所以更改服务器响应以返回一些内容

{
    android:[
        { ver:"1.5", name:"Cupcace", api:"Api Level 3" }
        ...
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

建议:了解您在改造中request/response添加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);
    }
Run Code Online (Sandbox Code Playgroud)

另外别忘了添加:

compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
Run Code Online (Sandbox Code Playgroud)

  • 使用这个,它会告诉你为什么你的json是错的http://jsonlint.com/ (2认同)

And*_*mon 10

使用 Moshi:

在构建您的改造服务时,将 .asLenient() 添加到您的 MoshiConverterFactory。您不需要 ScalarsConverter。它应该是这样的:

return Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(ENDPOINT)
                .addConverterFactory(MoshiConverterFactory.create().asLenient())
                .build()
                .create(UserService::class.java)
Run Code Online (Sandbox Code Playgroud)


Ali*_*lli 8

此问题也出现在响应内容类型不是时application/json.在我的情况下,响应contenttype是text/html,我遇到了这个问题.我改变它application/json然后它工作.


小智 8

对返回类型的理解有错误 只需添加 Header 即可解决您的问题

@Headers("Content-Type: application/json")
Run Code Online (Sandbox Code Playgroud)


Raj*_*Raj 5

我在/sf/answers/4007154091/上遇到了同样的问题,并且在修复了google-services.json之后都解决了