Advertisement
Guest User

android json parse

a guest
Jul 16th, 2019
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package com.example.myapplication;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.widget.ListView;
  8.  
  9. import org.json.JSONArray;
  10. import org.json.JSONException;
  11. import org.json.JSONObject;
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.IOException;
  15. import java.io.InputStream;
  16. import java.io.InputStreamReader;
  17. import java.net.HttpURLConnection;
  18. import java.net.MalformedURLException;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. public class Main3Activity extends AppCompatActivity {
  24.     private ListView listView;
  25.     String JsonFile;
  26.     @Override
  27.     protected void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_main3);
  30.         listView = (ListView)findViewById(R.id.list_view);
  31.         JsonTask jsonTask = new JsonTask();
  32.         jsonTask.execute();
  33.     }
  34.  
  35.     public class JsonTask extends AsyncTask<String,String, List<DataDemo>>{
  36.  
  37.         @Override
  38.         protected  List<DataDemo> doInBackground(String... strings) {
  39.  
  40.             HttpURLConnection httpURLConnection = null;
  41.             BufferedReader bufferedReader = null;
  42.  
  43.             try {
  44.                 URL url = new URL("https://gis.taiwan.net.tw/XMLReleaseALL_public/scenic_spot_C_f.json");
  45.                 httpURLConnection = (HttpURLConnection) url.openConnection();
  46.                 httpURLConnection.connect();
  47.                 InputStream inputStream = httpURLConnection.getInputStream();
  48.                 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  49.                 StringBuffer stringBuffer = new StringBuffer();
  50.                 String line ="";
  51.                 while ((line=bufferedReader.readLine())!=null){
  52.                     stringBuffer.append(line);
  53.                 }
  54.                 JsonFile = stringBuffer.toString();
  55.                 JSONObject firstObject = new JSONObject(JsonFile);
  56.                 JSONArray getArray = firstObject.getJSONObject("XML_Head").getJSONObject("Infos").getJSONArray("Info");
  57.                 List<DataDemo> DataList = new ArrayList<>();
  58.                 for (int i = 0;i<getArray.length();i++){
  59.                     JSONObject ArrayObject = getArray.getJSONObject(i);
  60.                     DataDemo data = new DataDemo();
  61.                     String a = ArrayObject.getString("Add");
  62.                     if(a.charAt(0)=='台'&& a.charAt(1)=='東'){
  63.                         data.setName(ArrayObject.getString("Name"));
  64.                         data.setTel(ArrayObject.getString("Tel"));
  65.                         data.setAdd(ArrayObject.getString("Add"));
  66.                         DataList.add(data);
  67.                     }
  68.                 }
  69.                 return DataList;
  70.  
  71.                 //return stringBuffer.toString();
  72.  
  73.  
  74.             } catch (MalformedURLException e) {
  75.                 e.printStackTrace();
  76.             } catch (IOException e) {
  77.                 e.printStackTrace();
  78.             } catch (JSONException e) {
  79.                 e.printStackTrace();
  80.             } finally {
  81.  
  82.                 try {
  83.                     httpURLConnection.disconnect();
  84.                     bufferedReader.close();
  85.                 } catch (IOException e) {
  86.                     e.printStackTrace();
  87.                 }
  88.             }
  89.  
  90.  
  91.             return null;
  92.         }
  93.  
  94.         @Override
  95.         protected void onPostExecute(List<DataDemo> s) {
  96.             super.onPostExecute(s);
  97.  
  98.             CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(),R.layout.simple,s);
  99.             listView.setAdapter(customAdapter);
  100.  
  101.  
  102.         }
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement