KarneeKarnay
Newbie
I'm creating an app that goes through JSON data on a website and then presents it to the user. It asks the user to enter a game name and searches through the JSON database to get the results. I'm having some problems with getting the results though. It seems like certain results crash the app when I click on them, but I don't get any errors.
Java:
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private Button button = null;
private EditText text = null;
private ProgressBar progressBar = null;
private TextView responseView = null;
private ListView gameListRows = null;
ImageView imageView = null;
private String query_text = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.s_button);
text = (EditText) findViewById(R.id.s_text);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
responseView = (TextView) findViewById(R.id.responseView);
gameListRows = (ListView) findViewById(R.id.listView);
imageView = (ImageView) findViewById(R.id.icon);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
query_text = text.getText().toString();
new JSONParser().execute();
}
});
}
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=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();
}
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)
{
try {
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;
}catch (Exception e)
{
System.out.println(e);
}
}
}
);
}
}
}