#1 Posted by mricewallace (4 posts) -

Currently working on an android app project for my uni, I'm trying to link the GiantBomb API to my app, but I keep hitting an error. My App exists currently of a search bar and a button. The idea is that the user types in the game's name and the app returns the Game and it's ID. I do this by using an XML parser that is separated into three java classes.   I have been following TheNewBoston android app tutorials. http://thenewboston.org/watch.php?cat=6&number=154. The current problem is that the app always returns "error" instead. Anyone got a clue as to what I'm doing wrong?
 
 [code]package com.example.xmltest; 

import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
static final String baseURL = "http://api.giantbomb.com/games/?api_key=b0d8a8ba77b4308775bb3d3b7829f1339c4de9b8&format=xml";
TextView tv;
EditText city;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        Button b = (Button)findViewById(R.id.bWeather);
        tv = (TextView)findViewById(R.id.tvWeather);
        city = (EditText)findViewById(R.id.etCity);
        b.setOnClickListener(this);
    }
    
    public void onClick(View v){
   
    String c = city.getText().toString();
   
    StringBuilder URL = new StringBuilder(baseURL);
    URL.append(c);
    String fullURL = URL.toString();
   
    try{
    URL website = new URL(fullURL);
   
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    HandlingXMLStuff doingWork = new HandlingXMLStuff();
    xr.setContentHandler(doingWork);
    xr.parse(new InputSource(website.openStream()));
    String information = doingWork.getInformation();
    tv.setText(information);
   
    }catch (Exception e){
    tv.setText("error");
    }
    }
}
[/code]  
  
[code]
package com.example.xmltest;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class HandlingXMLStuff extends DefaultHandler{

private XMLDataCollected info = new XMLDataCollected();
public String getInformation(){
return info.dataToString();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equals("aliases"))
{
String city = attributes.getValue("aliases");
info.setcity(city);
}
else if (localName.equals("id"))
{
String t = attributes.getValue("id");
int temp = Integer.parseInt(t);
info.setTemp(temp);
}
}

}  

 
[/code]  
  
[code]
 
 package com.example.xmltest;

public class XMLDataCollected {
String city = null;
int temp = 0;
public void setcity(String c){
city = c;
}
public void setTemp(int t){
temp = t;
}
public String dataToString(){
return city + " " + temp;
}
}
[/code]
#2 Edited by mricewallace (4 posts) -

UPDATE:

So I think the problem is a combination of me misusing the API and not understanding how to retrieve the information properly. The problem I still have is that the app still returns an error. I think the problems is either that I'm not retreivng the data, or more likely I'm not handling it properly. If someone could tell me if they see anything wrong with this code please tell me. Specifically on how I get the API info:

//PARSER CLASS:

public class MainActivity extends Activity implements OnClickListener{

static final String baseURL = "http://api.giantbomb.com/search/?query=";

TextView tv;

EditText city;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button b = (Button)findViewById(R.id.bWeather);

tv = (TextView)findViewById(R.id.tvWeather);

city = (EditText)findViewById(R.id.etCity);

b.setOnClickListener(this);

}

public void onClick(View v){

String c = city.getText().toString();

StringBuilder URL = new StringBuilder(baseURL);

URL.append("c"+ "&field_list=name,id&resources=game,concept&api_key=b0d8a8ba77b4308775bb3d3b7829f1339c4de9b8&format=xml&offset=0");

String fullURL = URL.toString();

try{

URL website = new URL(fullURL);

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

HandlingXMLStuff doingWork = new HandlingXMLStuff();

xr.setContentHandler(doingWork);

xr.parse(new InputSource(website.openStream()));

String information = doingWork.getInformation();

tv.setText(information);

}catch (Exception e){

tv.setText("error");

}

}

}

//HOW I RETRIEVE THE DATA CLASS:

package com.example.xmltest;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class HandlingXMLStuff extends DefaultHandler{

private XMLDataCollected info = new XMLDataCollected();

public String getInformation(){

return info.dataToString();

}

@Override

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

// TODO Auto-generated method stub

if (localName.equals("city"))

{

String city = attributes.getValue("name");

info.setcity(city);

}

}

}

//HOW I HANDLE IT:

package com.example.xmltest;

public class XMLDataCollected {

String name = null;

public void setcity(String c){

name = c;

}

public String dataToString(){

return name;

}

}

#3 Posted by LordAndrew (14430 posts) -

You're catching every type of exception and setting the TextView to the least informative thing possible. For debugging purposes, catch only what you're required to catch and set the TextView (or toast or dialog or whatever works best) to something that makes it clear what exception was thrown.

For exceptions you are not required to catch, don't bother catching them. Let the application crash and check logcat for the stack trace. Once you know what's causing the error you can catch and handle the exception appropriately or prevent the exception from being thrown in the first place.

You can also print the stack trace to logcat with your existing code by calling e.printStackTrace() during the catch. Just don't forget to check the log for the trace.

#4 Posted by mricewallace (4 posts) -

@LordAndrew: Late I know but thanks for the help. I've decided to move onto a Json Parser. A friend of mine sent me an example of how to use it with twitter. I'm currently trying to modify it for my project. I think the probleam I have is that I've muddled up my JSONObjects and JSONArrays.

private void loadTweetsJSON(String search) throws Exception

{

results = downloadTweets("http://api.giantbomb.com/search/?query=" + URLEncoder.encode(search, "UTF-8") + "&field_list=name,id&resources=game,concept&api_key=b0d8a8ba77b4308775bb3d3b7829f1339c4de9b8&format=json&offset=0");

System.out.println(results.toString());

JSONObject jsonResults = new JSONObject(results);

JSONArray resultsArray = jsonResults.getJSONArray("results");

listItems = new ArrayList<String>();

for (int i = 0; i < resultsArray.length(); i++)

{

JSONObject j = (JSONObject) resultsArray.get(i);

listItems.add(j.getString("name") + ": " + j.getString("text"));

}

handler.post(displayResultsRunnable);

}

#5 Edited by mricewallace (4 posts) -

So I've done some more debuging and discovered the problem is with the "0". It never increases. There is stuff in the array it just doesn't seem to get the array's length or it just ignores it. I've used the eclipse debugger to look at the values displayed in the "resultsArray" and it does indeed contain the all the information stored in the "results" array from the API.

My understanding is because of this problem with "i", it never downloads the contents of the array, and therefor there can't be anything to put as the text value or on the ListView in my program.

No that I defiantly know the problem is with the for loop, how do I get this to work?

[IMG]http://i47.tinypic.com/29pew6v.png[/IMG]

private void loadTweetsJSON(String search) throws Exception

{

results = downloadTweets("http://api.giantbomb.com/search/?query=" + URLEncoder.encode(search, "UTF-8") + "&field_list=name,id&resources=game,concept&api_key=1234556789&format=json&offset=0");

System.out.println(results.toString());

JSONObject jsonResults = new JSONObject(results);

JSONArray resultsArray = jsonResults.getJSONArray("results");

listItems = new ArrayList<String>();

for (int i = 0; i < resultsArray.length(); i++)

{

JSONObject j = (JSONObject) resultsArray.get(i);

listItems.add(j.getString("name") + ": " + j.getString("text"));

}

handler.post(displayResultsRunnable);

}

EDIT: Found the problem.

listItems.add(j.getString("name") + ": " + j.getString("text"));

This line should be:

listItems.add(j.getString("name") );

:P