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]
Log in to comment