JSON data not getting loaded into custom listview - android

I am not able to load the custom listview with my json response. Here is my code.
// my activity
public class LocalExploreActivity extends Activity {
ListView lvEvents;
JSONAdapter adapter;
JSONArray eventResponseArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_explore);
lvEvents = (ListView) findViewById(R.id.eventsList);
adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);
lvEvents.setAdapter(adapter);
}
private void getEventsList() throws JSONException {
JSONObject jsonObjSend = new JSONObject();
JSONObject jsonObjRecv = HttpGetClient.SendHttpPost(URL);
eventResponseArray = jsonObjRecv.getJSONArray("events");
System.out.println("events array issssssssssss "+eventResponseArray);
System.out.println("array len "+eventResponseArray.length());
JSONObject parkObj;
JSONObject activityObj;
JSONArray sessionArray;
for (int i = 0; i < eventResponseArray.length(); i++) {
JSONObject eventDetails = eventResponseArray.getJSONObject(i);
parkObj = eventDetails.getJSONObject("park");
activityObj = eventDetails.getJSONObject("activity");
eventName = eventDetails.getString("name");
System.out.println("the eventName is 33333 "+eventName);
eventFee = eventDetails.getString("fee");
System.out.println("the eventFee is 33333 "+eventFee);
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
EventModel.PREF_EVENTNAME = prefs.getString(eventName, eventName);
EventModel.PREF_EVENTFEE = prefs.getString(eventFee, eventFee);
}
}
}
// my Adapter
public class JSONAdapter extends BaseAdapter implements ListAdapter{
private final Activity activity;
private final JSONArray jsonArray;
protected JSONAdapter (Activity activity, JSONArray jsonArray) {
assert activity != null;
assert jsonArray != null;
this.jsonArray = jsonArray;
this.activity = activity;
}
#Override public int getCount() {
if(null==jsonArray)
return 0;
else
return jsonArray.length();
}
#Override public JSONObject getItem(int position) {
if(null==jsonArray) return null;
else
return jsonArray.optJSONObject(position);
}
#Override public long getItemId(int position) {
JSONObject jsonObject = getItem(position);
return jsonObject.optLong("id");
}
#Override public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = activity.getLayoutInflater().inflate(R.layout.custom_events_list, null);
TextView txtEventName = (TextView)convertView.findViewById(R.id.eventTitleTV);
TextView txtEventFee = (TextView)convertView.findViewById(R.id.eventPriceTV);
JSONObject json_data = getItem(position);
System.out.println("the json data received is 111111111 "+json_data);
if(null!=json_data ){
String eventName = null;
String eventFee = null;
try {
eventName = json_data.getString("name");
eventFee=json_data.getString("fee");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtEventName.setText(eventName);
txtEventFee.setText(eventFee);
}
return convertView;
}
}
When I run the app in the Debug mode, and check for the value in the below code,
adapter = new JSONAdapter (LocalExploreActivity.this,eventResponseArray);
The array is null. Need Help!!

You don't appear to be initializing the eventResponseArray anywhere so of course its null.

Related

ArrayList returning null after the data saved to ArrayList?

I am working in setting the data to the spinner by using the custom adapter, in that i am getting the data from web server json data are saved to the list but when i set the data the saved data to the textview it shows exception list is null
public class PhotoCommnFragment extends android.support.v4.app.Fragment {
EditText rechargeMobileNumber,rechargeAmount;
Spinner selectMenu;
int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
List<SpinnerMenu> selectedNetwork = new ArrayList<>();
public PhotoCommnFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
mobileRecahrgeHistory();
rechargeMobileNumber = (EditText)rootView.findViewById(R.id.recharge_mobile_number);
rechargeAmount = (EditText)rootView.findViewById(R.id.recharge_amount);
selectMenu = (Spinner)rootView.findViewById(R.id.selectNetwork);
settingSpinnerDropDown();
return rootView;
}
public void mobileRecahrgeHistory(){
Ion.with(this)
.load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
.asJsonObject().withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
#Override
public void onCompleted(Exception e, Response<JsonObject> result) {
JSONObject json = null;
try {
json = new JSONObject(result.getResult().toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = null;
jsonRootObject = json.optJSONObject("DS");
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("LST");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
} catch (JSONException e1) {
e1.printStackTrace();
}
String iph = null;
String oid = jsonObject.optString("OID").toString();
String ocd = jsonObject.optString("OCD").toString();
String opd = jsonObject.optString("OPE").toString();
String mil = jsonObject.optString("MIL").toString();
String mxl = jsonObject.optString("MXL").toString();
try {
iph = jsonObject.getString("IPH").toString();
} catch (JSONException e1) {
e1.printStackTrace();
}
String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e3) {
e3.printStackTrace();
}
SpinnerMenu spinnerData = new SpinnerMenu();
spinnerData.setOid(oid);
spinnerData.setOcd(ocd);
spinnerData.setOpd(opd);
spinnerData.setMil(mil);
spinnerData.setMix(mxl);
spinnerData.setImage(mIcon11);
selectedNetwork.add(spinnerData);
}
}
});
}
public void settingSpinnerDropDown(){
Fragment_DTH_Main_Spinner_Adapter customAdapter=new Fragment_DTH_Main_Spinner_Adapter(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork);
selectMenu.setAdapter(customAdapter);
}
}
The data first set to the SpinnerMenu(Model Class) and then model class data is set to the list.
This is my custom adapter for setting data to spinner
public class Fragment_DTH_Main_Spinner_Adapter extends ArrayAdapter<SpinnerMenu> {
Context context;
int flags[];
List<SpinnerMenu> countryNames;
LayoutInflater inflter;
public Fragment_DTH_Main_Spinner_Adapter(FragmentActivity activity, int resouceId, int textviewId, List<SpinnerMenu> data) {
// super(activity, R.layout.fragment_dth_main_spinner_items,userstories);
super(activity,resouceId,textviewId,data);
this.countryNames = data;
}
public class ViewHolder
{
TextView names;
ImageView icon;
}
// #Override
public int getCount() {
return countryNames.size();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fragment_dth_main_spinner_items, parent, false);
// convertView = inflter.inflate(fragment_dth_main_spinner_items, null);
SpinnerMenu spinnerData = countryNames.get(position);
ViewHolder viewHolder;
View result;
if(convertView == null){
viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) convertView.findViewById(R.id.imageView);
viewHolder.names = (TextView) convertView.findViewById(R.id.serviceName);
result = convertView;
}
else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
viewHolder.icon.setImageBitmap(spinnerData.getImage());
viewHolder.names.setText(spinnerData.getOpd());
return convertView;
}
}
Please help me how to solve this

Arraylist is returning null in asynchronous method?

I am facing the problem in JSON data from server is saved in the ArrayList is returning null when the ArrayList is called in the custom adapter. The custom adapter is used to list the data in the spinner in android.
public class PhotoCommnFragment extends android.support.v4.app.Fragment {
EditText rechargeMobileNumber,rechargeAmount;
Spinner selectMenu;
int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
List<SpinnerMenu> selectedNetwork = new ArrayList<>();
public PhotoCommnFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
mobileRecahrgeHistory();
rechargeMobileNumber = (EditText)rootView.findViewById(R.id.recharge_mobile_number);
rechargeAmount = (EditText)rootView.findViewById(R.id.recharge_amount);
selectMenu = (Spinner)rootView.findViewById(R.id.selectNetwork);
settingSpinnerDropDown();
return rootView;
}
public void mobileRecahrgeHistory(){
Ion.with(this)
.load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
.asJsonObject().withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
#Override
public void onCompleted(Exception e, Response<JsonObject> result) {
JSONObject json = null;
try {
json = new JSONObject(result.getResult().toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = null;
jsonRootObject = json.optJSONObject("DS");
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("LST");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
} catch (JSONException e1) {
e1.printStackTrace();
}
String iph = null;
String oid = jsonObject.optString("OID").toString();
String ocd = jsonObject.optString("OCD").toString();
String opd = jsonObject.optString("OPE").toString();
String mil = jsonObject.optString("MIL").toString();
String mxl = jsonObject.optString("MXL").toString();
try {
iph = jsonObject.getString("IPH").toString();
} catch (JSONException e1) {
e1.printStackTrace();
}
String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e3) {
e3.printStackTrace();
}
SpinnerMenu spinnerData = new SpinnerMenu();
spinnerData.setOid(oid);
spinnerData.setOcd(ocd);
spinnerData.setOpd(opd);
spinnerData.setMil(mil);
spinnerData.setMix(mxl);
spinnerData.setImage(mIcon11);
selectedNetwork.add(spinnerData);
}
}
});
}
public void settingSpinnerDropDown(){
Fragment_DTH_Main_Spinner_Adapter customAdapter=new Fragment_DTH_Main_Spinner_Adapter(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork);
selectMenu.setAdapter(customAdapter);
}
in the aove method the data from internet are saved in arraylist but when the arraylist is called in Fragment_DTH_Main_Spinner_Adapter` the arraylist is returning null. I checked by debugging the datas are save in arraylist.
I refferred in stackoverflow that you should add the
Adapter.notifyDataSetChanged() in your method. I don't know where to add this method . In my method the the asynchronous method called before calling the custom adapter . Then what is the use of using this method. Please help me.
The below code is Fragment_DTH_Main_Spinner_Adapter adapter class
public class Fragment_DTH_Main_Spinner_Adapter extends ArrayAdapter<SpinnerMenu> {
Context context;
int flags[];
List<SpinnerMenu> countryNames;
LayoutInflater inflter;
public Fragment_DTH_Main_Spinner_Adapter(FragmentActivity activity, int resouceId, int textviewId, List<SpinnerMenu> data) {
// super(activity, R.layout.fragment_dth_main_spinner_items,userstories);
super(activity,resouceId,textviewId,data);
this.countryNames = data;
}
public class ViewHolder
{
TextView names;
ImageView icon;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fragment_dth_main_spinner_items, parent, false);
// convertView = inflter.inflate(fragment_dth_main_spinner_items, null);
SpinnerMenu spinnerData = countryNames.get(position);
ViewHolder viewHolder;
View result;
if(convertView == null){
viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) convertView.findViewById(R.id.imageView);
viewHolder.names = (TextView) convertView.findViewById(R.id.serviceName);
result = convertView;
}
else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
// viewHolder.icon.setImageBitmap(spinnerData.getImage());
// Glide.with(context).load(spinnerData.getImages()).into(viewHolder.icon);
viewHolder.names.setText(spinnerData.getOpd());
return convertView;
}
}
Make your customadapter global and call notifydatasetchanged in oncomplete
public class PhotoCommnFragment extends android.support.v4.app.Fragment {
EditText rechargeMobileNumber, rechargeAmount;
Spinner selectMenu;
int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
List<SpinnerMenu> selectedNetwork = new ArrayList<>();
Fragment_DTH_Main_Spinner_Adapter customAdapter;
public PhotoCommnFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
mobileRecahrgeHistory();
rechargeMobileNumber = (EditText) rootView.findViewById(R.id.recharge_mobile_number);
rechargeAmount = (EditText) rootView.findViewById(R.id.recharge_amount);
selectMenu = (Spinner) rootView.findViewById(R.id.selectNetwork);
settingSpinnerDropDown();
customAdapter = new Fragment_DTH_Main_Spinner_Adapter(getActivity(), R.layout.fragment_dth_main_spinner_items, R.id.serviceName, selectedNetwork);
return rootView;
}
public void mobileRecahrgeHistory() {
Ion.with(this)
.load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
.asJsonObject().withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
#Override
public void onCompleted(Exception e, Response<JsonObject> result) {
JSONObject json = null;
try {
json = new JSONObject(result.getResult().toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = null;
jsonRootObject = json.optJSONObject("DS");
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("LST");
//Iterate the jsonArray and print the info of JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
} catch (JSONException e1) {
e1.printStackTrace();
}
String iph = null;
String oid = jsonObject.optString("OID").toString();
String ocd = jsonObject.optString("OCD").toString();
String opd = jsonObject.optString("OPE").toString();
String mil = jsonObject.optString("MIL").toString();
String mxl = jsonObject.optString("MXL").toString();
try {
iph = jsonObject.getString("IPH").toString();
} catch (JSONException e1) {
e1.printStackTrace();
}
String urldisplay = "http://192.168.1.105/TotalRecharge/" + iph;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e3) {
e3.printStackTrace();
}
SpinnerMenu spinnerData = new SpinnerMenu();
spinnerData.setOid(oid);
spinnerData.setOcd(ocd);
spinnerData.setOpd(opd);
spinnerData.setMil(mil);
spinnerData.setMix(mxl);
spinnerData.setImage(mIcon11);
selectedNetwork.add(spinnerData);
customAdapter.notifyDataSetChanged();
}
}
});
}
public void settingSpinnerDropDown() {
selectMenu.setAdapter(customAdapter);
}

AsyncTask + JSON parsing to Listview only sometimes shows data

Sometimes it shows data, sometimes it shows nothing.
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment, container, false);
arraylist1 = new ArrayList<>();
listOfNews = (ListView) view.findViewById(R.id.listView);
runOnUiThread(new Runnable() {
#Override
public void run() {
new ReadJSON().execute("url");
}
});
return view;
}
public class ReadJSON extends AsyncTask<String, Integer, String> {
#Override
protected String doInBackground(String... url) {
String st = readURL(url[0]);
Log.d("st", st);
return st;
}
#Override
protected void onPostExecute(String content) {
try {
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
Log.d(i+":jsonArray object", jsonArray.toString() + " size= " + jsonArray.length());
JSONObject productObject = jsonArray.getJSONObject(i);
JSONObject img = productObject.getJSONObject("image");
JSONObject da = img.getJSONObject("data");
arraylist1.add(new HotNews(
da.getString("filename"),
productObject.getString("title"),
productObject.getString("article_by"),
productObject.getString("date_publish"),
productObject.getString("short_description")
));
}
Log.d("jsonArray news list1", arraylist1.size() + "");
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new ListViewAdapter(view.getContext(), R.layout.list_view_adapter, arraylist1);
listOfNews.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
//create url object
URL url = new URL(theUrl);
//create url connection
URLConnection urlConnection = url.openConnection();
//wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return content.toString();
}
}
public class ListViewAdapter extends ArrayAdapter<HotNews> {
public ListViewAdapter(Context context, int resource, ArrayList<HotNews> arrayList) {
super(context, resource,arrayList);
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.arrayList = arrayList;
this.context = context;
}
#Override
public int getCount() {
return arrayList.size();
}
#Override
public int getViewTypeCount() {
return 2;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.list_view_adapter, parent, false);
}
image = (ImageView) v.findViewById(R.id.imagetitle);
title = (TextView) v.findViewById(R.id.txtTitle);
doer = (TextView) v.findViewById(R.id.doer);
date = (TextView) v.findViewById(R.id.txtdate);
text = (TextView) v.findViewById(R.id.text);
final HotNews news = getItem(position);
Picasso.with(getContext()).load("url" + news.getImage()).noFade().into(image);
title.setText(news.getTitle());
doer.setText(news.getDoer());
date.setText(news.getDate());
text.setText(news.getContent());
return v;
}
}
try this code :
public class HomeFragment extends Fragment {
private View view;
private ArrayList<HotNews> arraylist1;
private ListView listOfNews;
private int SUCCESS = 1;
private int FAILS = 0;
private int NO_DATA = 2;
private int ERROR = -1;
private ListViewAdapter adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragment, container, false);
arraylist1 = new ArrayList<>();
listOfNews = (ListView) view.findViewById(R.id.listView);
adapter = new ListViewAdapter();
listOfNews.setAdapter(adapter);
new ReadJSON().execute("url");
return view;
}
public class ReadJSON extends AsyncTask<String, Integer, Integer> {
#Override
protected Integer doInBackground(String... url) {
String response = readURL(url[0]);
Log.d("response ", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
arraylist1.clear();
if(jsonArray.length()>0) {
for (int i = 0; i < jsonArray.length(); i++) {
Log.d(i + ":jsonArray object", jsonArray.toString() + " size= " + jsonArray.length());
JSONObject productObject = jsonArray.getJSONObject(i);
JSONObject img = productObject.getJSONObject("image");
JSONObject da = img.getJSONObject("data");
arraylist1.add(new HotNews(
da.getString("filename"),
productObject.getString("title"),
productObject.getString("article_by"),
productObject.getString("date_publish"),
productObject.getString("short_description")
));
}
}else{
return NO_DATA;
}
Log.d("jsonArray news list1", arraylist1.size() + "");
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
#Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
Log.d("parseResponse", integer + "");
if(integer==SUCCESS){
adapter.notifyDataSetChanged();
}else if(integer==ERROR){
Toast.makeText(getActivity(),"Server or Exception Error!", Toast.LENGTH_SHORT).show();
}else if(integer==NO_DATA){
Toast.makeText(getActivity(),"No Data!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),"Some thing went wrong!", Toast.LENGTH_SHORT).show();
}
}
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
return arraylist1.size();
}
#Override
public Object getItem(int position) {
return arraylist1.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ListViewHolder holder;
if (v == null) {
v = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_adapter, parent, false);
holder = new ListViewHolder(v);
v.setTag(holder);
}else{
holder = (ListViewHolder)v.getTag();
}
final HotNews news = arraylist1.get(position);
holder.title.setText(news.getTitle());
holder.doer.setText(news.getDoer());
holder.date.setText(news.getDate());
holder.text.setText(news.getContent());
Picasso.with(getContext()).load("url" + news.getImage()).noFade().into(holder.image);
return v;
}
class ListViewHolder {
private ImageView image;
private TextView title,doer,date,text;
public ListViewHolder(View v){
image = (ImageView) v.findViewById(R.id.imagetitle);
title = (TextView) v.findViewById(R.id.txtTitle);
doer = (TextView) v.findViewById(R.id.doer);
date = (TextView) v.findViewById(R.id.txtdate);
text = (TextView) v.findViewById(R.id.text);
}
}
}
private static String readURL(String theUrl) {
StringBuilder content = new StringBuilder();
try {
//create url object
URL url = new URL(theUrl);
//create url connection
URLConnection urlConnection = url.openConnection();
//wrap the urlconnection in a bufferedreader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
}
catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
}
You are seeing this error, which is crashing the entire loop. You may be requesting 10 objects, but at the first object without that image attribute, it is exiting.
W/System.err: org.json.JSONException: No value for image
So, recommendation (other than using Retrofit to manage your JSON API's) would be to use the optType() methods rather than the getType() methods of the JSON objects.
For example,
JSONObject img = productObject.optJSONObject("image");
This will set img to null when the "image" attribute does not exist.
Just be aware of that later in case you have a NullPointerException when trying to use img on this line
JSONObject da = img.getJSONObject("data");

Android - Display data from Adapter in Listview

I've currently got an application that pulls data from a mysql database and displays it in raw JSON format. I'm currently working on pushing this data into a String variable and displaying it on a Listview on a specific activity.
Problem is, when trying to display this data, my Listview is not populating; I'm sure the variable is not empty as the if statement would have captured this.
Here is snippet of MainActivity code:
//Methods to grab information from abhandym_DB database
public void getJSON(View view){
new BackgroundTask().execute();
}
public void parseJSON(View view){
if(JSON_String==null){
Toast.makeText(getApplicationContext(), "First Get Json", Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(this,Test.class);
intent.putExtra("JSON_Data",JSON_String);
startActivity(intent);
}
}
class BackgroundTask extends AsyncTask<Void,Void,String>{
String json_url;
#Override
protected void onPreExecute() {
json_url = "http://abhandyman.x10host.com/json_get_data.php";
}
#Override
protected String doInBackground(Void... params) {
try {
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream inputSteam = httpURLConnection.getInputStream();
BufferedReader buffereredReader = new BufferedReader(new InputStreamReader(inputSteam));
StringBuilder stringBuilder = new StringBuilder();
while((JSON_String = buffereredReader.readLine())!=null){
stringBuilder.append(JSON_String+"\n");
}
buffereredReader.close();
inputSteam.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
#Override
protected void onPostExecute(String result) {
TextView textView = (TextView)findViewById(R.id.fragment1_textview_JSONAPPEAR);
textView.setText(result);
JSON_String = result;
}
}
Here is the code for my Test.java
public class Test extends AppCompatActivity {
String JSON_String;
JSONObject jsonObject;
JSONArray jsonArray;
DataAdapter dataAdapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
listView = (ListView)findViewById(R.id.test_listView);
dataAdapter = new DataAdapter(this, R.layout.row_layout);
listView.setAdapter(dataAdapter);
JSON_String = getIntent().getExtras().getString("JSON_Data");
try {
jsonObject = new JSONObject(JSON_String);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String jobid,problem,resolution;
while(count<jsonObject.length()){
JSONObject JO = jsonArray.getJSONObject(count);
jobid = JO.getString("jobid");
problem = JO.getString("problem");
resolution = JO.getString("resolution");
Data data = new Data(jobid,problem,resolution);
dataAdapter.add(data);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Here is the code for my DataAdapter:
public class DataAdapter extends ArrayAdapter{
List list = new ArrayList();
public DataAdapter(Context context, int resource) {
super(context, resource);
}
public void add(Data object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHolder dataHolder;
if(row == null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout,parent,false);
dataHolder = new DataHolder();
dataHolder.tx_jobid = (TextView) row.findViewById(R.id.tx_jobid);
dataHolder.tx_problem = (TextView) row.findViewById(R.id.tx_problem);
dataHolder.tx_resolution = (TextView) row.findViewById(R.id.tx_resolution);
row.setTag(dataHolder);
}else{
dataHolder = (DataHolder)row.getTag();
}
Data data = (Data)this.getItem(position);
dataHolder.tx_jobid.setText(data.getJobid());
dataHolder.tx_problem.setText(data.getProblem());
dataHolder.tx_resolution.setText(data.getResolution());
return row;
}
static class DataHolder{
TextView tx_jobid,tx_problem,tx_resolution;
}
}
and here is what it displays when clicking on "Parse JSON" button.
listView empty after population
Any help or advise on why its not displaying would be much appreciated!
Thanks in advance!
your problem seems to be here :
while(count<jsonObject.length()){
you're not looping using the number of array elements but using the number of mapped key:value object which is one (the "server_response") , you have to change this line to :
while(count<jsonArray.length()){
,
you have just the first element showing because jsonObject.length() will return 1 since it have just one element.
from the doc, JSONObject, length() method:
Returns the number of name/value mappings in this object.
and in your case you have just one name/value mapped ("server_response":[array items...])
Check in Test.java. I think You are setting the adapter to the listview before adding data to it
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
listView = (ListView)findViewById(R.id.test_listView);
dataAdapter = new DataAdapter(this, R.layout.row_layout);
JSON_String = getIntent().getExtras().getString("JSON_Data");
try {
jsonObject = new JSONObject(JSON_String);
jsonArray = jsonObject.getJSONArray("server_response");
int count = 0;
String jobid,problem,resolution;
while(count<jsonObject.length()){
JSONObject JO = jsonArray.getJSONObject(count);
jobid = JO.getString("jobid");
problem = JO.getString("problem");
resolution = JO.getString("resolution");
Data data = new Data(jobid,problem,resolution);
dataAdapter.add(data);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
listView.setAdapter(dataAdapter); //change effected
}

Reading JSON array to listview

I'm pretty new to android dev and I need some help.
I'm building an agenda that loads the information from a JSON then a ListView is inflated with a custom adapter. I've done this and works just fine.
My problem is the following when I click a contact another Activity is loaded with more information about the user, using the same JSON. I debug it and it recieves the information like this:
Example Item: [{"id":1,"name":"Leanne Graham","hobby":"Play soccer","address":"Kulas Light, Gwenborough","phone":"1-770-736-8031 x56442"}]
Because I sent the information as a JSONObject I cast it to be a JSONArray, but when I pass that array to my requestComplete my app breaks.
The error is:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
/**Main activity onclick listener*/
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("POSITION: " + position);
JSONObject jsonObject = (JSONObject)JSONadapter.getItem(position);
Intent intent = new Intent(this, InfoActivity.class);
String pos_json = jsonObject.toString();
intent.putExtra("pos_json",pos_json);
startActivity(intent);
}
/**Info activity*/
public class InfoActivity extends AppCompatActivity implements JSONRequest.JSONCallback {
AdapterInfo JSONAdapter;
private ListView listInfo;
private JSONObject json_object;
private JSONArray arrayMain;
private ArrayList<String> jsonarray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
JSONArray array = new JSONArray();
try {
json_object = new JSONObject(getIntent().getStringExtra("pos_json"));
arrayMain = array.put(json_object);
System.out.println("Example Item: "+ arrayMain.toString());
System.out.println(arrayMain.getClass().getName());
} catch (JSONException e) {
e.printStackTrace();
}
requestComplete(arrayMain);
this.listInfo = (ListView) findViewById(R.id.listView2);
}
#Override
public void requestComplete(JSONArray array) {
JSONAdapter = new AdapterInfo(InfoActivity.this,array);
this.listInfo.setAdapter(JSONAdapter);
}
/**Adapter*/
public class AdapterInfo extends BaseAdapter{
private JSONArray array;
private Activity infoAct;
public AdapterInfo(Activity infoAct, JSONArray array){
this.array = array;
this.infoAct = infoAct;
}
#Override
public int getCount() {
if(array == null){
return 0;
}else{
return array.length();
}
}
#Override
public JSONObject getItem(int position) {
if(array == null){
return null;
}else{
return array.optJSONObject(position);
}
}
#Override
public long getItemId(int position) {
JSONObject object = getItem(position);
return object.optLong("id");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = infoAct.getLayoutInflater().inflate(R.layout.row,null);
}
TextView name = (TextView)convertView.findViewById(R.id.infoName);
TextView hobby = (TextView)convertView.findViewById(R.id.infoHobby);
TextView address = (TextView)convertView.findViewById(R.id.infoAddress);
TextView phone = (TextView)convertView.findViewById(R.id.infoPhone);
JSONObject json_data = getItem(position);
if(json_data != null){
try {
String nombre = json_data.getString("name");
String pasatiempo = json_data.getString("hobby");
String direccion = json_data.getString("address");
String telefono = json_data.getString("phone");
name.setText(nombre);
hobby.setText(pasatiempo);
address.setText(direccion);
phone.setText(telefono);
} catch (JSONException e) {
e.printStackTrace();
}
}
return convertView;
}}
/**JSONRequest*/
public class JSONRequest extends AsyncTask<String, Void, JSONArray> {
private JSONCallback callback;
public JSONRequest(JSONCallback callback){
this.callback = callback;
}
#Override
protected JSONArray doInBackground(String... params) {
URLConnection connection = null;
BufferedReader br = null;
JSONArray result = null;
try{
URL url = new URL(params[0]);
connection = (URLConnection) url.openConnection();
InputStream is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is));
StringBuilder builder = new StringBuilder();
String line = "";
while((line = br.readLine()) != null){
builder.append(line);
}
result = new JSONArray(builder.toString());
}catch (Exception e) {
e.printStackTrace();
} finally {
try{
if(br != null) br.close();
}catch(Exception e) {
e.printStackTrace();
}
}
return result;
}
#Override
protected void onPostExecute(JSONArray jsonArray) {
super.onPostExecute(jsonArray);
callback.requestComplete(jsonArray);
}
public interface JSONCallback{
void requestComplete(JSONArray array);
}}
Your code:
requestComplete(arrayMain);
this.listInfo = (ListView) findViewById(R.id.listView2);
requestComplete() uses this.listInfo instance but this.listInfo is null because it is set after requestComplete(). So you need to switch their order.
this.listInfo = (ListView) findViewById(R.id.listView2);
requestComplete(arrayMain);
It is better if you just put it right after call to setContentView() just to make sure this.listInfo holds valid ListView instance.

Categories

Resources