[英]JSON parce in android (Issue in try{})
I have some troubles with parse JSON in my android app. 我在Android应用程序中解析JSON时遇到了一些麻烦。 This is URL " https://gesmetrics.ru/testpiers.php ", where i saved my data. 这是URL“ https://gesmetrics.ru/testpiers.php ”,我在其中保存了数据。 In AsyncTask i'm trying to parse, but in try{...} code stop working. 在AsyncTask中,我尝试解析,但是在try {...}中,代码停止工作。 I really trying to fix it, but i have no idea. 我真的想解决它,但我不知道。 This is my function. 这是我的职责 DataJSON - it is get and set for my data. DataJSON-为我的数据获取和设置。 That's all my code. 这就是我的全部代码。
public class CardViewFragment extends Fragment {
public static final String TAG = "CardViewFragment";
private List<DataJSON> dataJSONList;
private RecyclerView recyclerView;
RVAdapter rvAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_card_view_fragment, container, false);
dataJSONList = new ArrayList<>();
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
loadDataFromServer(0);
//testData();
rvAdapter = new RVAdapter(dataJSONList);
recyclerView.setAdapter(rvAdapter);
getActivity();
return rootView;
}
private void testData(){
//dataJSONList.add(new DataJSON(0, "RiverIsland", "Moscow", "Moscow is the capital of Russia"));
//dataJSONList.add(new DataJSON(1, "kek", "Lol", "Scream"));
DataJSON data2 = new DataJSON(0, "RiverIsland", "Moscow", "Moscow is the capital of Russia");
dataJSONList.add(data2);
}
String strJson;
private void loadDataFromServer(final int id) {
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>(){
@Override
protected String doInBackground(Void... voids) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://gesmetrics.ru/testpiers.php").build();
Response response;
try {
response = client.newCall(request).execute();
strJson = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return strJson;
}
@Override
protected void onPostExecute(String strJson) {
super.onPostExecute(strJson);
Log.d("myLog", strJson);
//DataJSON data2 = new DataJSON(0, "RiverIsland", "Moscow", "Moscow is the capital of Russia");
//dataJSONList.add(data2);
try {
JSONArray jsonArray = new JSONArray(strJson);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
int id = object.getInt("pierId");
String name = object.getString("pierName");
String region = object.getString("region");
String description = object.getString("description");
DataJSON data = new DataJSON(id, name, region, description);
//nothing happens here
dataJSONList.add(data);
}
} catch (JSONException e) {
System.out.println("Issue");
}
}
};
task.execute();
}
} }
Thank's a lot! 非常感谢!
Your Json response is invalid . 您的Json回应无效。 In the first description there is a double quote in middle which is causing the issue 在第一个描述中,中间用双引号引起了问题
Remove double quote after. 之后删除双引号。 One of the Adriatic" s
are place it as One of the Adriatic's
in the response One of the Adriatic" s
被放置为One of the Adriatic's
you can always check if your response is valid or not over here or here 您可以随时在此处或此处检查您的回复是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.