Question
Need assistance with the following java programing question. I am writing this code in Android studio: Step 6: Edit your DetailsFragment to display the external
Need assistance with the following java programing question. I am writing this code in Android studio:
Step 6: Edit your DetailsFragment to display the external JSON data to the fragment. We will be using the JSON data to update the View associated with the fragment. This means we want to process the JSON data in the same method where we are creating the view. To read the JSON data and add it to the Details Fragment:
Update the JSONResponseHandler class to create a method that will get the data for one item from the JSON file.
The method should return an HashMap
The method should be passed a set of JSON Data or should get JSON data by reading the file calling the readFile method. You will need to pass the readFile method getActivity() which gets the activity associated with the current fragment.
Once you have the data from the file (your JSON data) you perform the similar steps what you did in the handleResponse method (above) to parse the JSON data.
Use a JSONTokener to parse the JSON response into a Java object and then returns that top-level object, which in this case is a Map.
Next, the code extracts the value (a JSONArray) associated with the "key" associated with your JSON data, which will be an ordered list.
Since we only want one record from the JSON data, instead of using a loop, you will use json_array_varriable_name.get(myID) to get a set of data for one item in your JSON data - where myID is the ID passed to the Details fragment when you clicked an item on the list.
Finally, your method will get individual data fields from your JSON object and use those fields to populate your HashMap. To populate a HashMap is just like adding data to a Bundle or to an Intent:
HashMapm = new HashMap<>() ; m.put(EID_TAG, _EID); m.put(NAME_TAG, _NAME); //...
This is my JSONResponseHandler Class code:
package edu.ucdenver.contactlistultimate; import android.app.Activity; import android.widget.LinearLayout; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import static android.content.Context.MODE_PRIVATE; /** * Created by TylerJLefant on 11/14/17. */ public class JSONResponseHandler { private static final String CATALOG_TAG = "CATALOG"; private static final String ID_TAG = "ID"; private static final String FNAME_TAG = "FNAME"; private static final String LNAME_TAG = "LNAME"; private static final String EMAIL_TAG = "EMAILADDRESS"; private static final String PHOTO_TAG = "CONTACTPHOTO"; private static final String TAG = "JSONResponseHandler"; public int x = 1; public List handleResponse(String JSONResponse) throws IOException { List result = new ArrayList(); try { // Get top-level JSON Object JSONObject responseObject = (JSONObject) new JSONTokener( JSONResponse).nextValue(); // Extract value of "CATALOG" key -- a List JSONArray CATALOG = responseObject.getJSONArray(CATALOG_TAG); // Iterate over CATALOG list for (int idx = 0; idx < CATALOG.length(); idx++) { // Get single CATALOG data - a Map JSONObject CAT = (JSONObject) CATALOG.get(idx); // Summarize CATALOG data as a string and add it to // result result.add(CAT.get(FNAME_TAG) + " " + CAT.getString(LNAME_TAG)); } } catch (JSONException e) { e.printStackTrace(); } return result; } public static void writeFile(Activity a, String data) throws FileNotFoundException { FileOutputStream fos = a.openFileOutput("MyJSONData.txt", MODE_PRIVATE); PrintWriter pw = new PrintWriter(new BufferedWriter( new OutputStreamWriter(fos))); pw.println(data); pw.close(); } public static String readFile(Activity a) throws IOException { FileInputStream fis = a.openFileInput("MyJSONData.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringBuffer data1 = new StringBuffer(""); String line = ""; while (null != (line = br.readLine())) { data1.append(line); } return data1.toString(); } public static void main(String[] args) { HashMapm = new HashMap<>(); //Performed: Update the JSONResponseHandler class to create a method that //will get the data for one item from the JSON file. m.put(ID_TAG, "ID"); m.put(FNAME_TAG, "FNAME"); m.put(LNAME_TAG, "LNAME"); m.put(EMAIL_TAG, "EMAILADDRESS"); m.put(PHOTO_TAG, "CONTACTPHOTO"); } }
Any help you can provide would be much appreciated!
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started