簡體   English   中英

android中的JSON parce(在try {}中發布)

[英]JSON parce in android (Issue in try{})

我在Android應用程序中解析JSON時遇到了一些麻煩。 這是URL“ https://gesmetrics.ru/testpiers.php ”,我在其中保存了數據。 在AsyncTask中,我嘗試解析,但是在try {...}中,代碼停止工作。 我真的想解決它,但我不知道。 這是我的職責 DataJSON-為我的數據獲取和設置。 這就是我的全部代碼。

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

}

非常感謝!

您的Json回應無效。 在第一個描述中,中間用雙引號引起了問題

之后刪除雙引號。 One of the Adriatic" s被放置為One of the Adriatic's

您可以隨時在此處此處檢查您的回復是否有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM