您好我正在尝试创建一个可以从网址获取lat长的应用
http://akshay.site90.net/getLats.php?username=rakesh
结果将以
的形式出现{"lat":"30.1328064900","longitude":"77.2865304300"}
我使用Android Retrofit进行此操作.. 但是当我打开应用程序时,每次都会调用失败()的改造,而不是成功() 我不知道出了什么问题.. 它应该调用 onSucces()
interface api.java
package com.example.akshay.parentapp;
import retrofit.http.GET;
/**
* Created by Akshay on 9/8/2015.
*/
public interface api {
@GET("/getLats.php?username=rakesh")
public void getData( retrofit.Callback<getGSONData> response);
}
getGSONData.java
package com.example.akshay.parentapp;
import android.util.Log;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by Akshay on 9/8/2015.
*/
public class getGSONData {
@SerializedName("lat")
@Expose
public String lat;
@SerializedName("longitude")
@Expose
public String longitude;
public void getlat(String lat) {
this.lat = lat;
}
public Double setLat() {
Log.e("====LAT", lat);
return Double.parseDouble(lat);
}
public void getlongitude(String longitude) {
this.longitude = longitude;
}
public Double setLong() {
Log.e("====LONG", longitude);
return Double.parseDouble(longitude);
}
}
Map.java
package com.example.akshay.parentapp;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* Created by Akshay on 8/31/2015.
*/
public class Map extends FragmentActivity {
public final String URL = "http://akshay.site90.net";
LatLng prev = null;
LatLng current = null;
int flag = 0;
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.e("OnLocationChanged" , "===========0");
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(URL).build();
api api = restAdapter.create(api.class);
api.getData( new Callback<getGSONData>() {
@Override
public void success(getGSONData getGSONData, Response response) {
Double LAT = getGSONData.setLat();
Double LONG = getGSONData.setLong();
current = new LatLng(LAT , LONG);
Log.e("===LAT" , String.valueOf(LAT));
Log.e("===LONG" , String.valueOf(LONG));
if (flag == 0 & LAT!=null & LONG != null)
{
prev = current;
flag = 1;
}
CameraUpdate cm = CameraUpdateFactory.newLatLngZoom(current, 18);
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.color(Color.RED).add(prev,current).width(20).visible(true);
googleMap.addPolyline(polylineOptions);
googleMap.animateCamera(cm);
prev = current;
current = null;
}
@Override
public void failure(RetrofitError error) {
Log.e("====" , "Something gone wrong");
}
});
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
});
}
}
答案 0 :(得分:1)
我可以看到一些可能导致某些问题的事情:
1)您的网络API会将数据作为&#39; text / html&#39;而不是&#39; application / json&#39;返回,因此Retrofit可能无法正确读取。
2)尝试添加&#39; /&#39;在基本网址的末尾,所以它看起来像这样:
public final String URL = "http://akshay.site90.net/";
3)你的setter / getter在&#39; getGSONData&#39;类。你的getter正在设置值,你的setter正在获取值。
答案 1 :(得分:1)
打印
t.getCause()
在onFailed中让我知道帮助你
答案 2 :(得分:0)
全体感谢您的支持
网址正在返回
{"lat":"30.1329624300","longitude":"77.2864860400"}
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
所以我清除了我的免费托管000.webhost.com的In Activity Adds 这工作
答案 3 :(得分:0)
在改造中你可以很容易地做到这一点:
,Api.class:
public interface Api {
@GET("/getLats.php")
Call<List<getGSONData>> getData(@Query("username") String username);
}
,假设为ServiceGenerator.class:
public class ServiceGenerator {
public static final String API_BASE_URL = "http://your.api-base.url";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);
}
}
在您的活动中,要异步调用使用enqueue:
//prepare variable to contain result
List<getGSONData> resultList;
//create service
Api myApi = ServiceGenerator.createService(Api.class);
//instantiate the call and set parameters
Call<List<getGSONData>> call = myApi.getData("rakesh");
//enqueue for doing asynchronous, if you want to synchronous use
//execute().body() with try catch:
//
// resultList = call.execute().body();
call.enqueue(new Callback<List<getGSONData>>() {
@Override
public void onResponse(Response<List<getGSONData>> response,
Retrofit retrofit)
{
// handle success
//code 200 ..300
if(response.isSuccesful())
resultList = response.body();
}
@Override
public void onFailure(Throwable t)
{
// handle failure
}
});
答案 4 :(得分:0)
你的setter和getter方法有误,请查看。它应该像
public void setlat(String lat) {
this.lat = lat;
}
public Double getlat() {
Log.e("====LAT", lat);
return Double.parseDouble(lat);
}
尝试在改造中使用addConverterFactory(GsonConverterFactory.create())
,以便将其值直接映射到POJO
类。
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
答案 5 :(得分:0)
使用或调用Call<ResponseBody>
而不是创建的对象来显示所有JSON字符串,并确保在检查代码之前一切正常,因为在邮递员或在线站点中可能不会显示。 / p>