0

I didnt have much knowledge on Android, so please help to load it from sdcard..

my mainactivity is here..

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;

import android.os.Bundle;

import android.util.Log;

import android.widget.ListView;

 

public class MainActivity extends 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();

    }

}

}

Arun
  • 1
  • 2
  • You want to fetched JSON data from a file And that file is inside your local Storage ( SD Card ) ? Please add the JSON sample also for better understanding . – Piash Sarker Nov 15 '17 at 04:01
  • You seem to misunderstand the idea of `localhost`. Is your Android device running an HTTP server? (hint: it is not). You also don't need an Asynctask to read the filesystem of the device . – OneCricketeer Nov 15 '17 at 04:06
  • Iam running http server on my pc. Iam using local host ip4 address to parse a json data – Arun Nov 15 '17 at 04:49

0 Answers0