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

Apps JSON Object Handler Error & Layout Issue

I have created an app that searches through a game database, obtains the JSON version of that data and loads it into a list view. The user can then click on an item and be taken to another activity with more information. The application works and achieves that.

However I think that there might be some sort of conflict issue that I haven't seen or am currently not seeing. The first search works fine. Every search afterwards, if the user selects on an item the app crashes. The only error I am getting comes during the second search.

Code:
org.json.JSONException: Value null at image of type org.json.JSONObject$1 cannot be converted to JSONObject

The code I am using is the following.

Code:
 public class JSONParser extends AsyncTask<Void,Void,String> {

        private Exception exception;
        private GetJSONValues jsonValues = new GetJSONValues();
        private ArrayList<Game> gameData = jsonValues.gameData;

        protected void onPreExecute()
        {
            progressBar.setVisibility(View.VISIBLE);
            responseView.setText("");
        }

        protected String doInBackground(Void... urls)
        {
            try {

                URL url = new URL("http://www.giantbomb.com/api/search/?query=" + URLEncoder.encode(query_text, "UTF-8")
                        + "&field_list=name,id,deck,description,image&resources=game,concept&api_key=????&format=json&offset=0");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                try
                {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

                    StringBuilder stringBuilder = new StringBuilder();
                    String line;

                    while ((line = bufferedReader.readLine()) != null)
                    {
                        stringBuilder.append(line).append("\n");
                    }
                    bufferedReader.close();

                    String result = stringBuilder.toString();

                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONArray jsonArray = jsonObject.getJSONArray("results");
                        jsonValues.getGameList(jsonArray);
                        jsonValues.getValues();

                        Log.i("Test", result);
                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                    }
                    return  result;
                }
                finally {
                    urlConnection.disconnect();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return null;
            }

        }

        protected void onPostExecute(String response)
        {
            if(response == null)
            {
                response = "THERE WAS AN ERROR";
            }
            progressBar.setVisibility(View.GONE);
            //Log.i("INFO", response);
            RowAdapter adapter = new RowAdapter(MainActivity.this, gameData);
            gameListRows.setAdapter(adapter);

            gameListRows.setOnItemClickListener(
                    new AdapterView.OnItemClickListener()
                    {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                        {
                            Game gameInfo =   gameData.get(position);
                            Bitmap temp = gameInfo.imageUrl;
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            gameInfo.imageUrl.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            byte[] bytes = stream.toByteArray();

                            gameInfo.imageUrl = null;
                            Intent myIntent = new Intent(view.getContext(), game_info.class);
                            myIntent.putExtra("tempGame", gameInfo);
                            myIntent.putExtra("gameImage", bytes);
                            startActivity(myIntent);
                            gameInfo.imageUrl=temp;
                        }
                    }

            );
        }
    }

My JSON Pull class.

Java:
public class GetJSONValues {

    private JSONArray gameList;
    private String gameName;
    private String gameDeck;
    private String gameImgLink;
    private String gameDescription;
    public ArrayList<Game> gameData = new ArrayList<Game>();
    public ArrayList<Game> gameDataDetail = new ArrayList<Game>();

    public JSONArray getGameList (JSONArray tempList)
    {
        gameList = tempList;
        return gameList;
    }

    private String getGameName (String tempName)
    {
        //System.out.println(tempName);
        return gameName = tempName;
    }

    private String getGameDeck (String tempGameDesc)
    {
        return gameDeck = tempGameDesc;
    }

    private String getGameDescription (String tempGameDescription)
    {
        return gameDescription = tempGameDescription;
    }

    private String getGameImgLink (String tempImgLink)
    {
        return gameImgLink = tempImgLink;
    }

    public ArrayList getValues()
    {
        for (int i = 0; i < gameList.length(); i++)
        {
            try
            {
                JSONObject oneObject = gameList.getJSONObject(i);
                getGameName(oneObject.getString("name"));
                getGameDeck(oneObject.getString("deck"));
                getGameDescription(oneObject.getString("description"));
                //String temp = oneObject.getJSONArray("image").getJSONObject(0).getString("icon_url");
                getGameImgLink(oneObject.getJSONObject("image").getString("small_url"));

                Game gameInfo = new Game(gameName, gameDeck, downloadImage(gameImgLink), gameDescription);
                //Game gameInfoDetail = new Game(gameName,gameDeck,downloadImage(gameImgLink), gameDescription);
                try {
                    gameData.add(gameInfo);
                    //gameDataDetail.add(gameInfoDetail);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

                getGameDeck(oneObject.getString("deck"));
            }
            catch (JSONException e)
            {
                System.out.println("Error here!");
                System.out.println(e.toString());
                return null;
            }
        }
        return gameData;
    }

Any ideas what I'm doing wrong? I've only included the areas I think could be responsible.

Onto the layout. I've noticed that I'm getting some weird layout issues with the custom adapter I am using. What I want is the image next the title. Not the center. See below.

UhETa76.png


Activity code:

Code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_below="@+id/g_thumbnail"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/g_title"
    android:layout_alignEnd="@+id/g_title"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout 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="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/g_thumbnail"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:gravity="center"
            android:id="@+id/g_title"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/g_thumbnail"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:isScrollContainer="true"
            android:id="@+id/g_desc"
            android:layout_below="@+id/g_thumbnail"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/g_title"
            android:layout_alignEnd="@+id/g_title"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            />

    </RelativeLayout>
</ScrollView>
 
Whatever value is in variable result is causing problems at the following line I think

Code:
JSONObject jsonObject = new JSONObject(result);

Set a breakpoint at this line

Code:
String result = stringBuilder.toString();

because at some point result contains a String value which is not valid JSON
 
Back
Top Bottom