• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps create URL from JSON file download image and display to left of text

Pooveshin

Newbie
I am creating an android app, In the app data is read from a JSON file, using a list view the heading is displayed and when a user clicks the heading they are taken to a second screen with the rest of the data. I am having a problem with one aspect, in the JSON file is a part called image which shows the end of a URL (All stored in variables), how do I join this bit to the URL1 to create the full image url, download the image and display it to the left of the heading on the main screen, and since the first 2 JSON items do not have the image how would I load a default from the system (catch the error)?

It needs to start getting the images from the third item as the first two do not have the image field.

I have attached my code below and the structure of the JSON file.

Code:
public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get Android Version Data JSON
private static String url = "http://codetest.cobi.co.za/androids.json";
private static String url1 = "http://codetest.cobi.co.za/";

ArrayList<HashMap<String, String>> androidversions;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   androidversions = new ArrayList<>();

   lv = (ListView) findViewById(R.id.list);

   new GetVersions().execute();
}

private class GetVersions 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);

               // Getting JSON Array node
               JSONArray versions = jsonObj.getJSONArray("versions");

               // looping through All Versions
               for (int i = 0; i < versions.length(); i++) {
                   JSONObject c = versions.getJSONObject(i);

                   String name = c.getString("name");
                   String version = c.getString("version");
                   String released = c.getString("released");
                   String api = c.getString("api");
                   String image = c.getString("image");

                   // tmp hash map for single version
                   HashMap<String, String> Version = new HashMap<>();

                   // adding each child node to HashMap key => value
                   Version.put("name", name);
                   Version.put("version", version);
                   Version.put("released", released);
                   Version.put("api", api);
                   Version.put("image",image);


                   // adding Data to version list
                   androidversions.add(Version);
               }
           } 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) {
       lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               HashMap<String, String> version = androidversions.get(position);
               String name = version.get("name");
               String ver = version.get("version");
               String released = version.get("released");
               String api = version.get("api");

               Intent intent = new Intent(MainActivity.this, Second_Level.class);
               intent.putExtra("name",name);
               intent.putExtra("version",ver);
               intent.putExtra("released",released);
               intent.putExtra("api",api);
               MainActivity.this.startActivity(intent);
           }
       });
       super.onPostExecute(result);
       // Dismiss the progress dialog
       if (pDialog.isShowing())
           pDialog.dismiss();
       /**
        * Updating parsed JSON data into ListView
        * */
       ListAdapter adapter = new SimpleAdapter(
               MainActivity.this, androidversions,
               R.layout.list_item, new String[]{"name"}, new int[]{name});

       lv.setAdapter(adapter);
    }

   }
}

Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">

<ImageView
   android:layout_width="75dp"
   android:layout_height="75dp"
   android:id="@+id/image"/>

<TextView
   android:id="@+id/name"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:paddingBottom="2dip"
   android:paddingTop="6dip"
   android:textColor="@color/colorPrimaryDark"
   android:textSize="16sp"
   android:textStyle="bold" />

<TextView
   android:id="@+id/version"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:paddingBottom="2dip"
   android:textColor="@color/colorAccent" />

<TextView
   android:id="@+id/released"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#5d5d5d"
   android:textStyle="bold" />

<TextView
   android:id="@+id/api"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textColor="#5d5d5d"
   android:textStyle="bold" />

</LinearLayout>

Code:
"versions": [
   {
       "name":"Alpha",
       "version":"1.0",
       "released":"September 23, 2008",
       "api":"1"
   },
   {
       "name":"Beta",
       "version":"1.1",
       "released":"February 9, 2009",
       "api":"2"
   },
   {
       "name":"Cupcake",
       "version":"1.5",
       "released":"April 27, 2009",
       "api":"3",
       "image":"images/cupcake.jpg"
 
Here's the simple way to join two strings

Code:
String url1="http://codetest.cobi.co.za/";
String url2="images/cupcake.jpg";

String url = url1 + url2;

As for downloading the image, I posted a link in your other thread which shows how to do this.
 
Back
Top Bottom