这是什么错误?我怎样才能解决这个问题?我的应用程序正在运行,但无法加载数据。这是我的错误:使用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;
}
}
RequestInterface:
public interface RequestInterface {
@GET("Erfan/news.php")
Call<JSONResponse> getJSON();
}
更新
总是这个错误与您的json无关,可能是来自错误的请求,为了更好地处理,首先请在邮递员中检查您的请求(如果您得到了响应),然后将您的json响应与您的模型进行比较,如果没有错的话,则该错误来自错误的请求,当您的响应未启动时也可能发生(在某些情况下响应可能是html)
System.out.println
,是吗?在Android中,您可以使用Log
该类,但这无关紧要。您没有获取数据,或者在附近发生错误JSONResponse jsonResponse = response.body();
。我不知道如何解决您的错误,因为它可能与网络相关。您应该能够自己检查该值。
response.body()