1. Download our Official Android App: Forums for Android!

Apps JSON Object Handler Error & Layout Issue

Discussion in 'Android Development' started by KarneeKarnay, Mar 21, 2016.

  1. KarneeKarnay

    KarneeKarnay Newbie
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    24
    Joined:
    Dec 3, 2012

    Dec 3, 2012
    24
    0
    15
    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 (Text):
    1. 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 (Text):
    1.  public class JSONParser extends AsyncTask<Void,Void,String> {
    2.  
    3.         private Exception exception;
    4.         private GetJSONValues jsonValues = new GetJSONValues();
    5.         private ArrayList<Game> gameData = jsonValues.gameData;
    6.  
    7.         protected void onPreExecute()
    8.         {
    9.             progressBar.setVisibility(View.VISIBLE);
    10.             responseView.setText("");
    11.         }
    12.  
    13.         protected String doInBackground(Void... urls)
    14.         {
    15.             try {
    16.  
    17.                 URL url = new URL("http://www.giantbomb.com/api/search/?query=" + URLEncoder.encode(query_text, "UTF-8")
    18.                         + "&field_list=name,id,deck,description,image&resources=game,concept&api_key=????&format=json&offset=0");
    19.                 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    20.  
    21.                 try
    22.                 {
    23.                     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    24.  
    25.                     StringBuilder stringBuilder = new StringBuilder();
    26.                     String line;
    27.  
    28.                     while ((line = bufferedReader.readLine()) != null)
    29.                     {
    30.                         stringBuilder.append(line).append("\n");
    31.                     }
    32.                     bufferedReader.close();
    33.  
    34.                     String result = stringBuilder.toString();
    35.  
    36.                     try {
    37.                         JSONObject jsonObject = new JSONObject(result);
    38.                         JSONArray jsonArray = jsonObject.getJSONArray("results");
    39.                         jsonValues.getGameList(jsonArray);
    40.                         jsonValues.getValues();
    41.  
    42.                         Log.i("Test", result);
    43.                     }
    44.                     catch (JSONException e)
    45.                     {
    46.                         e.printStackTrace();
    47.                     }
    48.                     return  result;
    49.                 }
    50.                 finally {
    51.                     urlConnection.disconnect();
    52.                 }
    53.             }
    54.             catch (IOException e)
    55.             {
    56.                 e.printStackTrace();
    57.                 return null;
    58.             }
    59.  
    60.         }
    61.  
    62.         protected void onPostExecute(String response)
    63.         {
    64.             if(response == null)
    65.             {
    66.                 response = "THERE WAS AN ERROR";
    67.             }
    68.             progressBar.setVisibility(View.GONE);
    69.             //Log.i("INFO", response);
    70.             RowAdapter adapter = new RowAdapter(MainActivity.this, gameData);
    71.             gameListRows.setAdapter(adapter);
    72.  
    73.             gameListRows.setOnItemClickListener(
    74.                     new AdapterView.OnItemClickListener()
    75.                     {
    76.                         @Override
    77.                         public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    78.                         {
    79.                             Game gameInfo =   gameData.get(position);
    80.                             Bitmap temp = gameInfo.imageUrl;
    81.                             ByteArrayOutputStream stream = new ByteArrayOutputStream();
    82.                             gameInfo.imageUrl.compress(Bitmap.CompressFormat.PNG, 100, stream);
    83.                             byte[] bytes = stream.toByteArray();
    84.  
    85.                             gameInfo.imageUrl = null;
    86.                             Intent myIntent = new Intent(view.getContext(), game_info.class);
    87.                             myIntent.putExtra("tempGame", gameInfo);
    88.                             myIntent.putExtra("gameImage", bytes);
    89.                             startActivity(myIntent);
    90.                             gameInfo.imageUrl=temp;
    91.                         }
    92.                     }
    93.  
    94.             );
    95.         }
    96.     }
    My JSON Pull class.

    Code (Java):
    1. public class GetJSONValues {
    2.  
    3.     private JSONArray gameList;
    4.     private String gameName;
    5.     private String gameDeck;
    6.     private String gameImgLink;
    7.     private String gameDescription;
    8.     public ArrayList<Game> gameData = new ArrayList<Game>();
    9.     public ArrayList<Game> gameDataDetail = new ArrayList<Game>();
    10.  
    11.     public JSONArray getGameList (JSONArray tempList)
    12.     {
    13.         gameList = tempList;
    14.         return gameList;
    15.     }
    16.  
    17.     private String getGameName (String tempName)
    18.     {
    19.         //System.out.println(tempName);
    20.         return gameName = tempName;
    21.     }
    22.  
    23.     private String getGameDeck (String tempGameDesc)
    24.     {
    25.         return gameDeck = tempGameDesc;
    26.     }
    27.  
    28.     private String getGameDescription (String tempGameDescription)
    29.     {
    30.         return gameDescription = tempGameDescription;
    31.     }
    32.  
    33.     private String getGameImgLink (String tempImgLink)
    34.     {
    35.         return gameImgLink = tempImgLink;
    36.     }
    37.  
    38.     public ArrayList getValues()
    39.     {
    40.         for (int i = 0; i < gameList.length(); i++)
    41.         {
    42.             try
    43.             {
    44.                 JSONObject oneObject = gameList.getJSONObject(i);
    45.                 getGameName(oneObject.getString("name"));
    46.                 getGameDeck(oneObject.getString("deck"));
    47.                 getGameDescription(oneObject.getString("description"));
    48.                 //String temp = oneObject.getJSONArray("image").getJSONObject(0).getString("icon_url");
    49.                 getGameImgLink(oneObject.getJSONObject("image").getString("small_url"));
    50.  
    51.                 Game gameInfo = new Game(gameName, gameDeck, downloadImage(gameImgLink), gameDescription);
    52.                 //Game gameInfoDetail = new Game(gameName,gameDeck,downloadImage(gameImgLink), gameDescription);
    53.                 try {
    54.                     gameData.add(gameInfo);
    55.                     //gameDataDetail.add(gameInfoDetail);
    56.                 }
    57.                 catch (Exception e)
    58.                 {
    59.                     e.printStackTrace();
    60.                 }
    61.  
    62.                 getGameDeck(oneObject.getString("deck"));
    63.             }
    64.             catch (JSONException e)
    65.             {
    66.                 System.out.println("Error here!");
    67.                 System.out.println(e.toString());
    68.                 return null;
    69.             }
    70.         }
    71.         return gameData;
    72.     }
    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.

    [​IMG]

    Activity code:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <ScrollView
    3.     android:layout_width="wrap_content"
    4.     android:layout_height="wrap_content"
    5.     android:id="@+id/scrollView"
    6.     android:layout_below="@+id/g_thumbnail"
    7.     android:layout_alignParentLeft="true"
    8.     android:layout_alignParentStart="true"
    9.     android:layout_alignParentBottom="true"
    10.     android:layout_alignRight="@+id/g_title"
    11.     android:layout_alignEnd="@+id/g_title"
    12.     xmlns:android="http://schemas.android.com/apk/res/android">
    13.  
    14.     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    15.         android:orientation="vertical" android:layout_width="match_parent"
    16.         android:layout_height="match_parent">
    17.  
    18.         <ImageView
    19.             android:layout_width="wrap_content"
    20.             android:layout_height="wrap_content"
    21.             android:id="@+id/g_thumbnail"
    22.             android:layout_alignParentTop="true"
    23.             android:layout_alignParentLeft="true"
    24.             android:layout_alignParentStart="true"
    25.             android:src="@drawable/ic_launcher" />
    26.  
    27.         <TextView
    28.             android:layout_width="wrap_content"
    29.             android:layout_height="wrap_content"
    30.             android:textAppearance="?android:attr/textAppearanceLarge"
    31.             android:text="Large Text"
    32.             android:gravity="center"
    33.             android:id="@+id/g_title"
    34.             android:layout_alignParentTop="true"
    35.             android:layout_toRightOf="@+id/g_thumbnail"
    36.             android:layout_alignParentRight="true"
    37.             android:layout_alignParentEnd="true" />
    38.  
    39.         <TextView
    40.             android:layout_width="wrap_content"
    41.             android:layout_height="wrap_content"
    42.             android:text="New Text"
    43.             android:isScrollContainer="true"
    44.             android:id="@+id/g_desc"
    45.             android:layout_below="@+id/g_thumbnail"
    46.             android:layout_alignParentBottom="true"
    47.             android:layout_alignRight="@+id/g_title"
    48.             android:layout_alignEnd="@+id/g_title"
    49.             android:layout_alignParentLeft="true"
    50.             android:layout_alignParentStart="true"
    51.             />
    52.  
    53.     </RelativeLayout>
    54. </ScrollView>
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,306
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,306
    10,410
    1,988
    Male
    Software developer
    South West of England
    Whatever value is in variable result is causing problems at the following line I think

    Code (Text):
    1.  
    2. JSONObject jsonObject = new JSONObject(result);
    3.  
    Set a breakpoint at this line

    Code (Text):
    1.  
    2. String result = stringBuilder.toString();
    3.  
    because at some point result contains a String value which is not valid JSON
     

Share This Page

Loading...