重用一些来自教程以使Retrofit正常工作。这些是关键类。 主要活动 package com.example.jc.retrofittest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.example.jc.retrofittest.POJO.Model;
import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;
public class MainActivity extends AppCompatActivity {
TextView city, status, humidity, pressure;
String url = "http://www.seecroatia.com/croatiamaps";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
city = (TextView) findViewById(R.id.txt_city);
status = (TextView) findViewById(R.id.txt_status);
humidity = (TextView) findViewById(R.id.txt_humidity);
pressure = (TextView) findViewById(R.id.txt_press);
//making object of RestAdapter
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(url).build();
city.setText(adapter.toString());
//Creating Rest Services
RestInterface restInterface = adapter.create(RestInterface.class);
//Calling method to get whether report
restInterface.getWheatherReport(new Callback<Model>() {
@Override
public void success(Model model, Response response) {
city.setText("jur1e");
// status.setText("Status :"+model.getTyopInfo());
}
@Override
public void failure(RetrofitError error) {
city.setText("greska");
String merror = error.getMessage();
}
});
}
}
Model.java
public class Model {
public Model() {
}
public Model(int idInfo, int typeInfo) {
this.idInfo = idInfo;
this.typeInfo = typeInfo;
}
public int getIdInfo() {
return idInfo;
}
public void setIdInfo(int idInfo) {
this.idInfo = idInfo;
}
public int getTypeInfo() {
return typeInfo;
}
public void setTypeInfo(int typeInfo) {
this.typeInfo = typeInfo;
}
private int idInfo;
private int typeInfo;
}
RestInterface
public interface RestInterface {
@GET("/getMarker.php")
void getWheatherReport(Callback<Model> cb);
}
饭店
public class Hotel {
private String idHotel;
private String pbr;
private String mjesto;
private String naziv;
private String adresa;
private String web;
private String email;
private String latitude;
private String longitude;
public Hotel() {
}
public Hotel(String idHotel, String pbr, String mjesto, String naziv, String adresa, String web, String email, String latitude, String longitude) {
this.idHotel = idHotel;
this.pbr = pbr;
this.mjesto = mjesto;
this.naziv = naziv;
this.adresa = adresa;
this.web = web;
this.email = email;
this.latitude = latitude;
this.longitude = longitude;
}
public String getIdHotel() {
return idHotel;
}
public void setIdHotel(String idHotel) {
this.idHotel = idHotel;
}
public String getPbr() {
return pbr;
}
public void setPbr(String pbr) {
this.pbr = pbr;
}
public String getMjesto() {
return mjesto;
}
public void setMjesto(String mjesto) {
this.mjesto = mjesto;
}
public String getNaziv() {
return naziv;
}
public void setNaziv(String naziv) {
this.naziv = naziv;
}
public String getAdresa() {
return adresa;
}
public void setAdresa(String adresa) {
this.adresa = adresa;
}
public String getWeb() {
return web;
}
public void setWeb(String web) {
this.web = web;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}
我的代码总是在Mainactivity中失败。建议好吗?
答案 0 :(得分:0)
现在获取java.Lang.IllegalStateException:预期BEGIN_OBJECT但是 是BEGIN_ARRAY。显然,我试图获得不同的JSON格式 它驻留在服务器上。我的Model类包含两个我认为的整数 从网络服务收集,但我的方式有问题 收到它。
端点返回JSONArray
,而不是JSONObject
。你的
Callback<Model>
应为Callback<List<Model>>
您还必须更改您的Model类。 idInfo
和typeInfo
返回String
而不是int