Reputation: 79
I wanted to import "latest.jason" from "OpenExchange rates" for latest currency rates.
But when I write "AsyncHttpClient" it creates the following "Un-Implemented Class":
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
// TODO Auto-generated method stub
}
But I wanted this- (to run)
public void onSuccess(String arg2) {
Log.i("MYFIRSTAPP" , "HTTP Successs");
try {
JSONObject jsonObj = new JSONObject(arg2);
JSONObject ratesObject = jsonObj.getJSONObject("rates");
Double gbpRate = ratesObject.getDouble("GBP");
Double eurRate = ratesObject.getDouble("EUR");
Log.i("MYFIRSTAPP", "GBP" +gbpRate);
Log.i("MYFIRSTAPP", "EUR" +eurRate);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The problem I''m getting is: The onSuccess is taking " int arg0, Header[] arg1, byte[] arg2 " as the parameters...
but i wanted - " String arg2 "
Upvotes: 1
Views: 191
Reputation: 9442
Try This...
AsyncHttpClient client = new AsyncHttpClient();
client.get(URL, new AsyncHttpResponseHandler()
{
@Override
public void onFailure(int statusCode, Header[] header, byte[] content,
Throwable error)
{
// show error messages here
super.onFailure(statusCode, error, content);
}
@Override
public void onSuccess(int statusCode, Header[] header, byte[] content)
{
if (statusCode == 200)
{
try
{
//convert byte[] to string.
String contentStr = content.toString();
Log.i("Tag", "content" + URL + " " + contentStr );
//String to JSONObject.
JSONObject jsonObj = new JSONObject(contentStr );
JSONObject ratesObject = jsonObj.getJSONObject("rates");
Double gbpRate = ratesObject.getDouble("GBP");
Double eurRate = ratesObject.getDouble("EUR");
Log.i("MYFIRSTAPP", "GBP" + gbpRate);
Log.i("MYFIRSTAPP", "EUR" + eurRate);
}
catch (Exception e)
{
e.toString();
}
}
else
{
// show network error toast here;
}
}
});
Upvotes: 0
Reputation: 3029
There are many variants of onSuccess
and onFailure
methods in AsyncHttpResponseHandler and its subclasses.
Most suitable for handling JSON data is in JsonHttpResponseHandler
Upvotes: 0