我正在尝试使用将解析JSON数组的Retrofit创建一个简单的应用程序,并将获取一些数据,如纬度和经度,但问题是返回null。我是Retrofit的新手。请解释您发布解决方案的内容
这是我的代码
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);
}
我试图从中获取经度和经度,但我没有得到任何结果
这是我的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
体)