Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.widget.ListView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- public class Main3Activity extends AppCompatActivity {
- private ListView listView;
- String JsonFile;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main3);
- listView = (ListView)findViewById(R.id.list_view);
- JsonTask jsonTask = new JsonTask();
- jsonTask.execute();
- }
- public class JsonTask extends AsyncTask<String,String, List<DataDemo>>{
- @Override
- protected List<DataDemo> doInBackground(String... strings) {
- HttpURLConnection httpURLConnection = null;
- BufferedReader bufferedReader = null;
- try {
- URL url = new URL("https://gis.taiwan.net.tw/XMLReleaseALL_public/scenic_spot_C_f.json");
- httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.connect();
- InputStream inputStream = httpURLConnection.getInputStream();
- bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- StringBuffer stringBuffer = new StringBuffer();
- String line ="";
- while ((line=bufferedReader.readLine())!=null){
- stringBuffer.append(line);
- }
- JsonFile = stringBuffer.toString();
- JSONObject firstObject = new JSONObject(JsonFile);
- JSONArray getArray = firstObject.getJSONObject("XML_Head").getJSONObject("Infos").getJSONArray("Info");
- List<DataDemo> DataList = new ArrayList<>();
- for (int i = 0;i<getArray.length();i++){
- JSONObject ArrayObject = getArray.getJSONObject(i);
- DataDemo data = new DataDemo();
- String a = ArrayObject.getString("Add");
- if(a.charAt(0)=='台'&& a.charAt(1)=='東'){
- data.setName(ArrayObject.getString("Name"));
- data.setTel(ArrayObject.getString("Tel"));
- data.setAdd(ArrayObject.getString("Add"));
- DataList.add(data);
- }
- }
- return DataList;
- //return stringBuffer.toString();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- } finally {
- try {
- httpURLConnection.disconnect();
- bufferedReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPostExecute(List<DataDemo> s) {
- super.onPostExecute(s);
- CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(),R.layout.simple,s);
- listView.setAdapter(customAdapter);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement