2017-07-15 2 views
1

私のアンドロイドアプリでJSONを解析する際にいくつか問題があります。 これはURL「https://gesmetrics.ru/testpiers.php」です。ここでデータを保存しました。 AsyncTaskで解析しようとしていますが、try {...}コードが機能しなくなりました。私は本当にそれを修正しようとしているが、私は考えていない。 これは私の機能です。 DataJSON - 自分のデータを取得して設定します。 それはすべて私のコードです。androidのJSON parce(try {}の問題)

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

}

感謝がたくさんです!

+0

上であなたがフラグエラーであなたのlogcatを投稿することができ、有効であるかどうかOne of the Adriatic" sが応答

One of the Adriatic'sような場所にあり、いつでも確認することができます。 –

+0

はい、エラーを投稿してください。 –

+0

あなたのjsonレスポンスは無効です – Redman

答えて

2

あなたのJsonレスポンスは無効です。 最初の説明に問題が発生している中間の二重引用符があります

二重引用符を削除してください。あなたの応答がhereまたはhere

+0

ありがとう!私はその問題を解決しますが、解析はまだ機能しません。 私はLogCatで問題がありません。 –

+0

すべてのことが正しいと思われる – Redman

+0

ログに返信していますか? – Redman