如何从sdcard而不是url加载json文本?

时间:2017-11-15 03:53:48

标签: android url android-asynctask android-sdcard android-json

我对Android没有太多了解,所以请帮助从SD卡加载..

我的主要活动就在这里..

import java.util.ArrayList;

import java.util.HashMap;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.app.Activity;

import android.app.ProgressDialog;

import android.os.AsyncTask;

导入android.os.Bundle;

import android.util.Log;

import android.widget.ListView;

公共类MainActivity扩展了Activity {

// Declare Variables

JSONObject jsonobject;

JSONArray jsonarray;

ListView listview;

ListViewAdapter adapter;

ProgressDialog mProgressDialog;

ArrayList<HashMap<String, String>> arraylist;

static String RANK = "rank";

static String COUNTRY = "country";

static String POPULATION = "population";

static String FLAG = "flag";

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // Get the view from listview_main.xml

    setContentView(R.layout.listview_main);

    // Execute DownloadJSON AsyncTask

    new DownloadJSON().execute();

}

// DownloadJSON AsyncTask

private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override

    protected void onPreExecute() {

        super.onPreExecute();

        // Create a progressdialog

        mProgressDialog = new ProgressDialog(MainActivity.this);

        // Set progressdialog title

        mProgressDialog.setTitle("Android JSON Parse Tutorial");

        // Set progressdialog message

        mProgressDialog.setMessage("Loading...");

        mProgressDialog.setIndeterminate(false);

        // Show progressdialog

        mProgressDialog.show();

    }

    @Override

    protected Void doInBackground(Void... params) {

        // Create an array

        arraylist = new ArrayList<HashMap<String, String>>();

        // Retrieve JSON Objects from the given URL address

        jsonobject = JSONfunctions

                .getJSONfromURL("http://localhost/jsonparse.txt");

        try {

            // Locate the array name in JSON

            jsonarray = jsonobject.getJSONArray("worldpopulation");

            for (int i = 0; i < jsonarray.length(); i++) {

                HashMap<String, String> map = new HashMap<String, String>();

                jsonobject = jsonarray.getJSONObject(i);

                // Retrive JSON Objects

                map.put("rank", jsonobject.getString("rank"));

                map.put("country", jsonobject.getString("country"));

                map.put("population", jsonobject.getString("population"));

                map.put("flag", jsonobject.getString("flag"));

                // Set the JSON Objects into the array

                arraylist.add(map);

            }

        } catch (JSONException e) {

            Log.e("Error", e.getMessage());

            e.printStackTrace();

        }

        return null;

    }

    @Override

    protected void onPostExecute(Void args) {

        // Locate the listview in listview_main.xml

        listview = (ListView) findViewById(R.id.listview);

        // Pass the results into ListViewAdapter.java

        adapter = new ListViewAdapter(MainActivity.this, arraylist);

        // Set the adapter to the ListView

        listview.setAdapter(adapter);

        // Close the progressdialog

        mProgressDialog.dismiss();

    }

}

}

0 个答案:

没有答案