1

Reused some come from tutorial to get Retrofit working. These are the key classes. MainActivity 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);

}

Hotel

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;
    }


}

My code allways goes to failure part in Mainactivity. Suggestions please?

Jure
  • 49
  • 3
  • 7
  • 1
    Please can you be more specific than "goes to failure"? Can you provide an exception stacktrace for example? – David G Oct 16 '15 at 08:19
  • i guess you don't have a ResultsDeserializer to adapt the received data to your Model. Post your log-output to see which error you get. – Carsten Drösser Oct 16 '15 at 08:38
  • Now gettint java.Lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY. Clearly I'm trying to get different JSON format that it resides on server. My Model class contains two ints that I'm collecting from web service, but something is wrong with the way I'm recieving it. – Jure Oct 16 '15 at 13:25

1 Answers1

0

Now getting java.Lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY. Clearly I'm trying to get different JSON format that it resides on server. My Model class contains two ints that I'm collecting from web service, but something is wrong with the way I'm recieving it.

The endpoint is returning a JSONArray, not a JSONObject. your

Callback<Model> should be Callback<List<Model>>

You have to change also your Model class. idInfo and typeInfo are returned as String not as int

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks a lot. I have one more question? How can I use this change this code to get JSON from different PHP scripts. In this case it is getting it from getMarker.php but I would like to send script name from MainActivity and use same interface all the time? What changes do I need to make? – Jure Oct 20 '15 at 08:46
  • I am not sure what you mean, but check [this example](http://stackoverflow.com/a/20725156/501696) out – Blackbelt Oct 20 '15 at 08:49
  • My static part of domain is www.xyz.com...I want to be able to get data from different scripts like getMarker1.php, getMarker2.php and to set that "dynamic" part of URL in the MainActivity... – Jure Oct 20 '15 at 09:04
  • you will have to add another method to your `RestInterface`. E.g. `@GET("/getMarke2r.php") void getMarkers(Callback cb);` ` – Blackbelt Oct 20 '15 at 09:06
  • I got different error now Expected BEGIN_ARRAY but was String...My web service is same, Model class contains ints, and Callback expects Array. Please help – Jure Oct 21 '15 at 20:27
  • `BEGIN_ARRAY but was String` means that your endpoint is returning a `JSONObject` and you registered a `Callback>` – Blackbelt Oct 21 '15 at 20:29
  • I have a strange behaviour on this, got the JSON normally and now getting again this error again(I really have not change any Retrofit code). My web service returns List (you can check it here http://www.seecroatia.com/croatiamaps/tzsibenik/getHotel.php).Please help – Jure Oct 22 '15 at 19:30
  • could you post the interface method ? – Blackbelt Oct 22 '15 at 19:35
  • This is the interface method @GET("/tzsibenik/getHotel.php") void getHotel(Callback> cb); – Jure Oct 22 '15 at 19:42
  • I keep getting the error Expected BEGIN_ARRAY but was STRING at line 2 column 11. – Jure Oct 22 '15 at 19:53
  • I really cant understand this, the code worked perfectly...I tried runing just Retrofit part and works fine, but in my example I still get this error. Any suggestions – Jure Oct 22 '15 at 20:37
  • I noticed that in emulator works fine, but this error ocurs on device – Jure Oct 22 '15 at 21:05
  • It doesn't make any sense to me.. Try rebooting your device or uninstall and reinstall the app again – Blackbelt Oct 22 '15 at 21:11
  • @ Blackbelt Do you have any clue on this - in emulator works fine, but I get an error on device? – Jure Oct 22 '15 at 21:12
  • Maybe you could try another device? – Blackbelt Oct 22 '15 at 21:20