Reputation: 150
I am trying to make a simple app using the Retrofit that will parse the JSON Array and will fetch some data such as latitude and longitude but the problem is that is is returning null. I am new to Retrofit. Please explain whatever you post the solution
Here is my code
MainActivity.java
package com.example.akshay.retrofitgsonjsonarray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class MainActivity extends AppCompatActivity {
public final String URL = "https://roads.googleapis.com/v1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(URL).setLogLevel(RestAdapter.LogLevel.FULL).build();
api api = restAdapter.create(api.class);
api.getData(new Callback<SnappedPoints>() {
@Override
public void success(SnappedPoints snappedPoints, Response response) {
Toast.makeText(MainActivity.this ,String.valueOf(snappedPoints.location.getLatitude()) , Toast.LENGTH_LONG ).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
});
}
}
Snappedpoints.java
package com.example.akshay.retrofitgsonjsonarray;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Akshay on 9/14/2015.
*/
public class SnappedPoints {
@SerializedName("location")
@Expose
Location location;
public void setLocation(Location location)
{
this.location = location;
}
public Location getLocation()
{
return location;
}
}
Location.java
package com.example.akshay.retrofitgsonjsonarray;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Akshay on 9/14/2015.
*/
public class Location {
@SerializedName("latitude")
@Expose
Double latitude;
@SerializedName("longitude")
@Expose
Double longitude;
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLatitude() {
return latitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLongitude() {
return longitude;
}
}
api.java
package com.example.akshay.retrofitgsonjsonarray;
import retrofit.Callback;
import retrofit.http.GET;
/**
* Created by Akshay on 9/8/2015.
*/
public interface api {
@GET("/snapToRoads?path=-35.27801,149.12958&interpolate=true&key=KEY")
public void getData( Callback<SnappedPoints> response);
}
I am Trying to get latitude and longitude out of it but I am not getting any result
Here is my Logcat:
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ <--- HTTP 200 https://roads.googleapis.com/v1/snapToRoads?path=-35.27801,149.12958&interpolate=true&key=KEYHERE_Ihavechangedthis (1901ms)
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ : HTTP/1.1 200 OK
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Alt-Svc: quic=":443"; p="1"; ma=604800
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Alternate-Protocol: 443:quic,p=1
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Cache-Control: private
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Content-Type: application/json; charset=UTF-8
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Date: Mon, 14 Sep 2015 09:23:17 GMT
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Server: ESF
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Transfer-Encoding: chunked
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Vary: Origin
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Vary: X-Origin
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ Vary: Referer
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ X-Android-Received-Millis: 1442222595815
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ X-Android-Response-Source: NETWORK 200
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ X-Android-Sent-Millis: 1442222595304
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ X-Content-Type-Options: nosniff
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ X-Frame-Options: SAMEORIGIN
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ X-XSS-Protection: 1; mode=block
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ {
"snappedPoints": [
{
"location": {
"latitude": -35.278005228392438,
"longitude": 149.12953569405369
},
"originalIndex": 0,
"placeId": "ChIJp47M1mdNFmsRcF5UbW7qABM"
}
]
}
09-14 14:53:15.820 19592-19636/com.example.akshay.retrofitgsonjsonarray D/Retrofit﹕ <--- END HTTP (224-byte
body)
Upvotes: 0
Views: 2095
Reputation: 10259
You have to define a container to meet the definition of your JSON:
public class SnappedPointsContainer {
@SerializedName("snappedPoints")
@Expose
List<SnappedPoints> snappedPoints;
// TODO Getter & Setter
}
and change your Retrofit-Interface to :
public interface api {
@GET("/snapToRoads?path=-35.27801,149.12958&interpolate=true&key=KEY")
public void getData( Callback<SnappedPointsContainer> response);
}
Define your callback this way:
api.getData(new Callback<SnappedPointsContainer>() {
@Override
public void success(SnappedPointsContainer snappedPointsContainer, Response response) {
// TODO Process e.g. snappedPointsContainer.getSnappedPoints()
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
});
Upvotes: 2
Reputation: 1090
I have no clue about retrofit, but it looks like your api is returning an array of values but you are trying to get it as scalar.
Try the following:
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(URL).setLogLevel(RestAdapter.LogLevel.FULL).build();
api api = restAdapter.create(api.class);
api.getData(new Callback<SnappedPoints[]>() {
@Override
public void success(SnappedPoints[] snappedPoints, Response response) {
Toast.makeText(MainActivity.this ,String.valueOf(snappedPoints[0].location.getLatitude()) , Toast.LENGTH_LONG ).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
});
Upvotes: 0