This is my first question and I need your help, there is a Fatal Error in my code. I´m trying to get a book data from google API using the doInBackground
method to manage it but the try-catch block is giving me null
.
I'm newbi in Android and I don't know how to solve this problem... please help me out :)
My code:
public class FrmSaludo extends Activity {
private String isbn;
private Book libro;
private TextView txtSaludo;
private Book resultado;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saludo);
// Localizar los controles
txtSaludo = (TextView) findViewById(R.id.TxtSaludo);
// Recuperamos la información pasada en el intent
Bundle bundle = this.getIntent().getExtras();
this.isbn = bundle.getString("ISBN");
Buscar();
/*
* OtherParse otherparse= new OtherParse(isbn);
* txtSaludo.setText("Hola " + otherparse.getResult());
*/
}
private class SearchIsbnTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
/*
* ParseJson parse= new ParseJson(params[0]); libro = parse.Parse();
*/
try{
OtherParse otherParse = new OtherParse(params[0]);
resultado = otherParse.getBook();
Log.v("TEST", "book ="+resultado.toString());
}catch (Exception e){
Log.e("BACKGROUND", "Error ejecutando hilo" + e.getMessage());
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.v("TEST", "volviendo a hilo principal");
if (result) {
txtSaludo.setText("Hola " + resultado.getTitle().toString());
}
}
}
public void Buscar() {
// Carga del XML mediante la tarea asíncrona
SearchIsbnTask tarea = new SearchIsbnTask();
tarea.execute(isbn);
}
}
public class OtherParse {
private String url;
private JSONObject jsonObject;
private String author;
private Book book;
public OtherParse(String isbn) {
HttpClient client = new DefaultHttpClient();
String ruta = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
this.url = ruta.concat(isbn);
HttpGet get = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
System.out.println("Buscando");
try {
responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
Log.v("RESPONSEBODY", "Exception: " + ex.getMessage());
}
this.jsonObject = null;
try {
this.jsonObject = new JSONObject(responseBody);
System.out.println("JSONRESPONSE =" + this.jsonObject);
} catch (Exception e) {
Log.v("TEST", "Exception: " + e.getMessage());
}
Book libro = new Book();
JSONArray jArray;
try {
jArray = jsonObject.getJSONArray("items");
for (int i = 0; i < jArray.length(); i++) {
JSONObject volumeInfo = jArray.getJSONObject(i).getJSONObject(
"volumeInfo");
libro.setTitle(volumeInfo.getString("title"));
JSONArray authors = volumeInfo.getJSONArray("authors");
for (int j = 0; j < authors.length(); j++) {
this.author = authors.getString(i);
}
libro.setAuthors(author);
}
System.out.println("TITULO=" + libro.getTitle().toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
JSONObject getResult(){
return this.jsonObject;
}
Book getBook(){
return this.book;
}
}