JSON Array Not Showing All Values - android

I'm using JSONParser.java to show JSON data from a url
the url and the data are correct, they show some values of JSON (more than one)
but why this show only one value?
ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(ArtikelURL);
try {
artikels = json.getJSONArray(TAG_ARTIKEL);
for(int i = 0; i < artikels.length(); i++){
JSONObject c = artikels.getJSONObject(i);
// Storing each json item in variable
String tanggal = c.getString(TAG_TGL);
String judul = c.getString(TAG_JUDUL);
String kutipan = c.getString(TAG_KUTIPAN);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TGL, tanggal);
map.put(TAG_JUDUL, judul);
map.put(TAG_KUTIPAN, kutipan);
artikelList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
This is my JSON data:
{
"status":"1",
"total":20,
"artikel":[
{
"tanggal":"2013-08-07",
"judul":"Article One",
"kutipan":"Example of article quote..."
},
{
"tanggal":"2013-07-23",
"judul":"Article Two",
"kutipan":"Example of article quote......"
},
{
"tanggal":"2013-07-22",
"judul":"Article Three",
"kutipan":"Example of article quote......"
},
{
"tanggal":"2013-03-16",
"judul":"Article Four"",
"kutipan":"Example of article quote,..."
}
]
}

Your JSON is invalid, there's an error in your line 22 at 3rd object of your array
"judul":"Article Four"",
Correct JSON is
{
"status": "1",
"total": 20,
"artikel": [
{
"tanggal": "2013-08-07",
"judul": "Article One",
"kutipan": "Example of article quote..."
},
{
"tanggal": "2013-07-23",
"judul": "Article Two",
"kutipan": "Example of article quote......"
},
{
"tanggal": "2013-07-22",
"judul": "Article Three",
"kutipan": "Example of article quote......"
},
{
"tanggal": "2013-03-16",
"judul": "Article Four",
"kutipan": "Exampleofarticlequote,..."
}
]
}
Ok, Now you are getting Network related exception so, solution to this is, you've to use demo code like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new NetworkThread().execute();
}
class NetworkThread extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... voids) {
ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(ArtikelURL);
try {
JSONArray artikels = json.getJSONArray("artikel");
for(int i = 0; i < artikels.length(); i++){
JSONObject c = artikels.getJSONObject(i);
// Storing each json item in variable
String tanggal = c.getString(TAG_TGL);
String judul = c.getString(TAG_JUDUL);
String kutipan = c.getString(TAG_KUTIPAN);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TGL, tanggal);
map.put(TAG_JUDUL, judul);
map.put(TAG_KUTIPAN, kutipan);
artikelList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
Don't forget to add this to AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"/>

Your JSON Is Invalid But assuming you have an outer Json Array for the JsonObject This implementation would work for your nested Json
ArrayList<HashMap<String, String>> artikelList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(ArtikelURL);
try {
JSONArray a = json.getJSONArray(TAG_JSON);
for(int i = 0; i < a.length(); i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = a.getJSONObject(i);
map.put(TAG_TOTAL, c.getString(TAG_TOTAL));
map.put(TAG_STATUS, c.getString(TAG_STATUS));
artikelList.add(map);
JSONArray akel = e.getJSONArray(artikel);
if(akel != null){
for(int j=0;j<akel.length();j++){
HashMap<String, String> map2 = new HashMap<String, String>();
JSONObject f = akel.getJSONObject(j);
map2.put(TAG_TGL, f.getString(TAG_TGL));
map2.put(TAG_JUDUL, f.getString(TAG_JUDUL));
map2.put(TAG_KUTIPAN, f.getString(TAG_KUTIPAN));
}
}
} catch (JSONException e) {
e.printStackTrace();
}

Related

error type JSONArray cannot be converted to JSONObject

I'm creating an app to get posts from a web server, and i'm getting a jsonarray to object error I'm new to android development and tutorials i see the JsonArray is named before, so it'd be array of dogs, and then inside that would have breed and name and so on, mines not named.
the code i have is
public class GetData extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... strings) {
String current = "";
try{
URL url;
HttpURLConnection urlConnection = null;
try{
url = new URL(JSONURL);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(in);
int data = isr.read();
while(data !=-1){
current += (char) data;
data = isr.read();
}
return current;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if(urlConnection !=null){
urlConnection.disconnect();;
}
}
}catch (Exception e){
e.printStackTrace();
}
return current;
}
#Override
protected void onPostExecute(String s) {
try{
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("");
for(int i = 0; i<jsonArray.length();i++){
JSONObject jsonObject1= jsonArray.getJSONObject(i);
namey = jsonObject1.getString("name");
post = jsonObject1.getString("post");
//hashmap
HashMap<String, String> posts = new HashMap<>();
posts.put("name", namey);
posts.put("post", post);
postList.add(posts);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Displaying the results
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,
postList,
R.layout.item,
new String[]{"name", "post"},
new int[]{R.id.textView, R.id.textView2});
lv.setAdapter(adapter);
}
}
the json code i'm trying to parse is
[
{
"0": "2",
"id": "2",
"1": "anon",
"name": "anon",
"2": "goodbye people of skivecore",
"post": "goodbye people of skivecore",
"3": "38.751053",
"lat": "38.751053",
"4": "-90.432915",
"lng": "-90.432915",
"5": "",
"ip": "",
"6": "6.204982836749738",
"distance": "6.204982836749738"
},
{
"0": "1",
"id": "1",
"1": "anon",
"name": "anon",
"2": "hello people of skivecore",
"post": "hello people of skivecore",
"3": "38.744453",
"lat": "38.744453",
"4": "-90.607986",
"lng": "-90.607986",
"5": "",
"ip": "",
"6": "9.280600590285143",
"distance": "9.280600590285143"
}
]
and stacktrace message
2021-04-28 23:45:02.156 20352-20352/com.skivecore.secrets W/System.err: org.json.JSONException: Value [{"0":"2","id":"2","1":"anon","name":"anon","2":"goodbye people of skivecore","post":"goodbye people of skivecore","3":"38.751053","lat":"38.751053","4":"-90.432915","lng":"-90.432915","5":"","ip":"","6":"6.204982836749738","distance":"6.204982836749738"},{"0":"1","id":"1","1":"anon","name":"anon","2":"hello people of skivecore","post":"hello people of skivecore","3":"38.744453","lat":"38.744453","4":"-90.607986","lng":"-90.607986","5":"","ip":"","6":"9.280600590285143","distance":"9.280600590285143"}] of type org.json.JSONArray cannot be converted to JSONObject
You should use like below
try {
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
namey = jsonObject1.getString("name");
post = jsonObject1.getString("post");
//hashmap
HashMap<String, String> posts = new HashMap<>();
posts.put("name", namey);
posts.put("post", post);
postList.add(posts);
}
} catch (JSONException e) {
e.printStackTrace();
}
I think here is the issue JSONObject jsonObject = new JSONObject(s); as I can see your response is a JSONArray because it has [ and ] at the beginning and the end.
So, I think this would be more appropriate.
try{
JSONArray jsonArray = jsonObject.getJSONArray(s);
for(int i = 0; i<jsonArray.length();i++){
JSONObject jsonObject1= jsonArray.getJSONObject(i);
namey = jsonObject1.getString("name");
post = jsonObject1.getString("post");
//hashmap
HashMap<String, String> posts = new HashMap<>();
posts.put("name", namey);
posts.put("post", post);
postList.add(posts);
}
}
please usding gson library To use jasonArray And JsonObject Easley
1). https://github.com/google/gson
2). libraby import then use
https://www.jsonschema2pojo.org/
for your jsonArray to Model class
Then Use jsonArray
Use this instead....
try {
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
namey = jsonObject1.getString("name");
post = jsonObject1.getString("post");
//put to hashmap
HashMap<String, String> posts = new HashMap<>();
posts.put("name", namey);
posts.put("post", post);
postList.add(posts);
}
} catch (JSONException e) {
e.printStackTrace();

issue with JSONArray cannot be converted to JSONObject

hi guys i don't know why but i get this error: JSONArray cannot be converted to JSONObject.
I show you my code!I hope that you can help me! thanks in advance everybody!
Sorry, but i am new to JSON.
MAIN ACTIVITY:
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = " http://v1.tvguideapi.com/programs?channels[]=2161&channels[]=2162&start=1484598600&stop=1484605799";
//private static String url = "http://api.androidhive.info/contacts/";
ArrayList<HashMap<String, String>> contactList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
//JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("programs");
// JSONArray contacts = jsonObj.getJSONArray("contacts");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
/* String id = c.getString("id");
String name = c.getString("name");
String email = c.getString("email");
String address = c.getString("address");
String gender = c.getString("gender");
// Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");*/
String title=c.getString("title");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
/* contact.put("id", id);
contact.put("name", name);
contact.put("email", email);
contact.put("mobile", mobile);*/
contact.put("title",title);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
/* MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile"}, new int[]{R.id.name,
R.id.email, R.id.mobile});*/
MainActivity.this, contactList,
R.layout.list_item, new String[]{"title"}, new int[]{R.id.title});
lv.setAdapter(adapter);
}
}
}
JSON:
[
{
"id": "18879971",
"start": "2017-01-16 20:20:00",
"stop": "2017-01-16 22:30:00",
"lang": "",
"title": "Il Collegio</td>",
"subtitle": "",
"description": "",
"category": "",
"channel_id": "2162",
"icon": null,
"ts_start": 1484598000,
"ts_stop": 1484605800
},
{
"id": "18879856",
"start": "2017-01-16 20:25:00",
"stop": "2017-01-16 22:40:00",
"lang": "",
"title": "I Bastardi di Pizzofalcone",
"subtitle": "",
"description": "Ep.3 - In un fatiscente condominio di Pizzofalcone viene rinvenuto il cadavere di una camariera. Le indagini della squadra portano al marito della donna, ma per Lojacono il caso si complica ulteriormente.",
"category": "",
"channel_id": "2161",
"icon": null,
"ts_start": 1484598300,
"ts_stop": 1484606400
}
]
You should use you have Json Array.
you have [{ },{}] format Here [] shows JsonArray and {} this is json object.
you can have Json array loop through array and get Json object.
JSONArray contacts = new JSONArray(jsonStr);
Instead of using
JSONObject jsonObj = new JSONObject(jsonStr);
Now you can fetch data
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
//Get all required data using c.
String id = c.getString("id");
String name = c.getString("start");
}

how to parse JsonArray and set value on Textview in Android

Below is my Json response from getting when Hit on Url with parsing parameter strlogid=101
[
{
"earnpoints": 411,
"type": "C"
},
{
"earnpoints": 1600,
"type": "G"
},
{
"earnpoints": 13540,
"type": "I"
}
]
Below is my code-To show Points on 3 different textview
private class PointTask extends AsyncTask<String, Void, Boolean> {
protected Boolean doInBackground(final String... args) {
JSONParse jParser = new JSONParse();
HashMap<String, String> hMap = new HashMap<>();
hMap.put("strlogid", "101");
JSONArray jsonarray = jParser.getJSONFromUrl(url,hMap);
// get JSON data from URL
for (int i = 0; i < jsonarray.length(); i++) {
try {
JSONObject c = (JSONObject) jsonarray.get(i);
c_point=c.getString("points");
g_point=c.getString("points");
i_point=c.getString("points");
t_point = cpoint+gpoint+ipoint;
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(final Boolean success) {
txtTotalPoints.setText(t_point);
txtOwnPoints.setText(g_point);
txtClubPoints.setText(c_point);
txtVoucherPoints.setText(i_point);
}
}
TotalPoints,OwnPoints,ClubPoints,VoucherPoints are show on Textview respectively
Try this
protected Boolean doInBackground(final String... args) {
JSONParse jParser = new JSONParse();
HashMap<String, String> hMap = new HashMap<>();
hMap.put("strlogid", "101");
JSONArray jsonarray = jParser.getJSONFromUrl(url,hMap);
// get JSON data from URL
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject c = (JSONObject) jsonarray.get(i);
if (c.getString("type").toString().equalsIgnoreCase("C")) {
c_point = c.getInt("earnpoints");
} else if (c.getString("type").toString().equalsIgnoreCase("G")) {
g_point = c.getInt("earnpoints");
} else if (c.getString("type").toString().equalsIgnoreCase("I")) {
i_point = c.getInt("earnpoints");
}
}
t_point = cpoint + gpoint + ipoint;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(final Boolean success) {
txtTotalPoints.setText(t_point);
txtOwnPoints.setText(g_point);
txtClubPoints.setText(c_point);
txtVoucherPoints.setText(i_point);
}
}
Dont use async task for json parsing. I would reccomend you to use volley.
Here is the link\
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
#Override
public void onPostSuccess(String result, int id, boolean isSucess) {
// Log.e("re<><><><>", result);
try {
JSONArray ja = new JSONArray(result);
for (int arr = 0; arr < ja.length(); arr++) {
JSONObject json1 = ja.getJSONObject(arr);
Log.e("length", "" + ja.length());
// Getting JSON Array node
String earnpoints= json1.getString("earnpoints");
String type = json1.getString("type");
}
catch (JSONException e) {
e.printStackTrace();
Toast.makeText(LoginPage.this, "Please try again later", Toast.LENGTH_SHORT).show();
}
}

Android: Best practice for handling JSON data

I've been researching how to query JSON data from a server and parse it for use in my application. However, I've found a number of different ways to do the same thing. I realize there are different JSON parsers out there, so let's assume I'm sticking with the standard one. The main question I have has to do with the server requests. Here is my current code for my MapActivity
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a Progress Dialog
mProgressDialog = new ProgressDialog(MapActivity.this);
mProgressDialog.setTitle("Downloading Data");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
try {
// Retrieve JSON Objects from the given URL address
jsonarray = JSONFunctions.getJSONfromURL("myurl");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject obj = jsonarray.getJSONObject(i);
// Retrieve JSON Objects
map.put("id", String.valueOf(i));
map.put("name", obj.getString("name"));
// 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) {
// Do something with data
mProgressDialog.dismiss();
}
}
If the structure of my JSON data looks weird, it's because it's stored in an unnamed array so I don't have to create an object first. Anyway... I essentially based this off of this tutorial. However, they have soooo much more code. Is that all really necessary? I didn't think so. I searched around more and found other examples that used half the code and essentially did the same thing. So my question, as a beginning Android programmer, is what is the best practice for handling JSON data? Thanks!
Example of JSON file:
[
{
"name": "test",
"lat": "42.01108",
"long": "93.679196"
},
{
"name": "test",
"lat": "42.01108",
"long": "93.679196"
}
]
Try this...
private class TestJSONParsing extends AsyncTask<JSONArray, Void, JSONArray>
{
ArrayList<HashMap<String, String>> arraylist;
HashMap<String, String> map;
#Override
protected void onPreExecute()
{
super.onPreExecute();
mProgressDialog = new ProgressDialog(MapActivity.this);
mProgressDialog.setTitle("Downloading Data");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected JSONArray doInBackground(JSONArray... params)
{
return JSONFunctions.getJSONfromURL("myurl");
}
#Override
protected void onPostExecute(JSONArray resultArray)
{
super.onPostExecute(resultArray);
mProgressDialog.dismiss();
if (null != resultArray)
{
int resultLength = resultArray.length();
if (resultLength > 0)
{
try
{
for (int i = 0; i < resultLength; i++)
{
JSONObject jsonObject = resultArray
.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", jsonObject.getString("name"));
arraylist.add(map);
}
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
if (arraylist.size() > 0)
{
SimpleAdapter adapter = new SimpleAdapter(
MapActivity.this, arraylist,
R.layout.your_simple_row, new String[]
{ "id", "name" }, new int[]
{ R.id.nameId, R.id.name });
// bind adapter to your components
listView.setAdapter(adapter);
}
}
} else
{
Toast.makeText(getApplicationContext(), "No data",
Toast.LENGTH_SHORT).show();
}
}
}
#leerob Hello, at one time I found myself in a dilemma where you are, but lately I have used base classes that brings Android to handle json and is pretty good, a good practice I recommend you is to declare constants for the keys of json, I share an example:
public void insertMovieTypeFromJson(String movieTypeJsonStr) throws JSONException {
final String ID = "id";
final String TYPE = "type";
final String DESCRIPTION = "description";
if (movieTypeJsonStr == null)
return;
try {
JSONArray movieTypeArrayJson = new JSONArray(movieTypeJsonStr);
Vector<ContentValues> cVVector = new Vector<>(movieTypeArrayJson.length());
for (int i = 0; i < movieTypeArrayJson.length(); i++) {
long id;
String type;
String description;
JSONObject movie = movieTypeArrayJson.getJSONObject(i);
id = movie.getLong(ID);
type = movie.getString(TYPE);
description = movie.getString(DESCRIPTION);
ContentValues movieTypeValues = new ContentValues();
movieTypeValues.put(MovieContract.MovieTypeEntry._ID, id);
movieTypeValues.put(MovieContract.MovieTypeEntry.COLUMN_TYPE, type);
movieTypeValues.put(MovieContract.MovieTypeEntry.COLUMN_DESCRIPTION, description);
cVVector.add(movieTypeValues);
}
int inserted = 0;
if (cVVector.size() > 0) {
ContentValues[] cvArray = new ContentValues[cVVector.size()];
cVVector.toArray(cvArray);
inserted = getContext().getContentResolver().bulkInsert(MovieContract.MovieTypeEntry.CONTENT_URI, cvArray);
}
Log.d(LOG_TAG, "MovieTask Complete. " + inserted + " MovieType Inserted");
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage(), e);
e.printStackTrace();
}
}
Json:
[
{
"id": "1",
"type": "Action & Adventure",
"description": "Action & Adventure"
},
{
"id": "2",
"type": "Animation",
"description": "Animation"
},
{
"id": "3",
"type": "Comedy",
"description": "Comedy"
},
{
"id": "4",
"type": "Terror",
"description": "Terror"
}
]

How to extract json data?

I am getting this trouble?
[
{
"username": "apap",
"status": "",
"log": "",
"email": "apap#gmail.com",
"lat": "",
"dob": "5-11-1986"
},
{
"username": "apapp",
"status": "",
"log": "",
"email": "apapp#gmail.com",
"lat": "",
"dob": "5-11-1986"
}
],
"returncode": "0"
}
Error parsing data org.json.JSONException: Value {"incircle": at response of type org.json.JSONObject cannot be converted to JSONArray
For extracting incircle array value,I am writing following code:
public class MainActivity extends Activity {
TextView username;
TextView email;
TextView dob;
TextView lat;
TextView log;
ListView listView;
ArrayList<HashMap<String,String>> jsonlist;
JsonAdapter objAdapter;
Context ctx;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView);
jsonlist = new ArrayList<HashMap<String,String>>();
JSONObject json = JSONfunctions
.getJSONfromURL("http://www.railsboxtech.com/flirtalert/alluser.php");
try {
JSONArray flirt = json.getJSONArray("response");
Toast.makeText(MainActivity.this, "namr", Toast.LENGTH_LONG).show();
for (int i = 0; i < flirt.length(); i++) {
JSONObject ei = flirt.getJSONObject(i);
JSONArray eie = ei.getJSONArray("incircle");
for (int j = 0; j < eie.length(); j++) {
JSONObject e = eie.getJSONObject(j);
HashMap<String, String> map = new HashMap<String, String>();
map.put("User",e.getString("username"));
map.put("Email",e.getString("email"));
map.put("dob",e.getString("dob"));
map.put("lat",e.getString("lat"));
map.put("log",e.getString("log"));
jsonlist.add(map);
}
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, jsonlist, R.layout.user_list,
new String[] { "User", "Email","dob","lat","log"}, new int[] {
R.id.username, R.id.email, R.id.dob, R.id.lat, R.id.log});
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
When you retrieve the elements of the array with this line
JSONArray ja1= eie.getJSONArray(j);
You require the elements to be arrays themselfs, however, in your input the elements are objects.
Since the input has only one array but your code assumes two nested arrays, you probably want something like:
JSONObject json = JSONfunctions
.getJSONfromURL("http://www.railsboxtech.com/flirtalert/alluser.php");
try {
JSONObject ei = json.getJSONObject("response");
JSONArray eie = ei.getJSONArray("incircle");
for (int j = 0; j < eie.length(); j++) {
JSONObject e = eie.getJSONObject(j);
HashMap<String, String> map = new HashMap<String, String>();
map.put("User",e.getString("username"));
map.put("Email",e.getString("email"));
map.put("dob",e.getString("dob"));
map.put("lat",e.getString("lat"));
map.put("log",e.getString("log"));
jsonlist.add(map);
}
} catch ...

Categories

Resources