从服务器改进响应OK 200,但我得到空值

时间:2017-08-05 21:00:36

标签: android json retrofit

这是我使用android和改装的第一步 我在MVC ASP API中构建了JSON代码 现在我不会从android调用这个服务 现在我试着打电话给它,但我看不到我布局中的任何数据 然后我添加了Break point来查看响应,所以现在我看到响应是: 响应{protocol = http / 1.1,code = 200,message = OK,url = http://www.travel-tourism1.somee.com/api/countries}

在身体中我可以看到json的对象,但只能键出值

like that:

我的代码是这样的:

package com.example.android.travelandtourism;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://www.travel-tourism1.somee.com/api/")
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    IApi service = retrofit.create(IApi.class);
    TextView name_En;
    TextView name_Ar;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        name_En = (TextView) findViewById(R.id.ivname_En);
        name_Ar = (TextView) findViewById(R.id.ivname_Ar);


        Call<Countries> call = service.getCountries();
        call.enqueue(new Callback<Countries>() {
            @Override
            public void onResponse(Call<Countries> call, Response<Countries> response) {
                Countries countryResonse = response.body();
                //name_En.setText(countryResonse.getNameEn().toString());
               // name_Ar.setText(countryResonse.getNameAr().toString());
            }

            @Override
            public void onFailure(Call<Countries> call, Throwable t){
            }
        });
}
}

package com.example.android.travelandtourism;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;



public class Countries {


    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name_En")
    @Expose
    private String nameEn;
    @SerializedName("name_Ar")
    @Expose
    private String nameAr;
    @SerializedName("flag")
    @Expose
    private String flag;
    @SerializedName("visible")
    @Expose
    private Boolean visible;
    @SerializedName("cities")
    @Expose
    public ArrayList<City> cities = null;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNameEn() {
        return nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    public String getNameAr() {
        return nameAr;
    }

    public void setNameAr(String nameAr) {
        this.nameAr = nameAr;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public Boolean getVisible() {
        return visible;
    }

    public void setVisible(Boolean visible) {
        this.visible = visible;
    }

    public ArrayList<City> getCities() {
        return cities;
    }

    public void setCities(ArrayList<City> cities) {
        this.cities = cities;
    }

}

package com.example.android.travelandtourism;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * Created by haya on 04/08/2017.
 */

public interface IApi {
    @GET("countries")
    Call <Countries> getCountries();


}

package com.example.android.travelandtourism;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class City {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("name_En")
    @Expose
    private String nameEn;
    @SerializedName("name_Ar")
    @Expose
    private String nameAr;
    @SerializedName("description_En")
    @Expose
    private String descriptionEn;
    @SerializedName("description_Ar")
    @Expose
    private String descriptionAr;
    @SerializedName("images")
    @Expose
    private ArrayList<Images> images = null;
    @SerializedName("cityLocation")
    @Expose
    private String cityLocation;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNameEn() {
        return nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    public String getNameAr() {
        return nameAr;
    }

    public void setNameAr(String nameAr) {
        this.nameAr = nameAr;
    }

    public String getDescriptionEn() {
        return descriptionEn;
    }

    public void setDescriptionEn(String descriptionEn) {
        this.descriptionEn = descriptionEn;
    }

    public String getDescriptionAr() {
        return descriptionAr;
    }

    public void setDescriptionAr(String descriptionAr) {
        this.descriptionAr = descriptionAr;
    }

    public ArrayList<Images> getImages() {
        return images;
    }

    public void setImages(ArrayList<Images> images) {
        this.images = images;
    }

    public String getCityLocation() {
        return cityLocation;
    }

    public void setCityLocation(String cityLocation) {
        this.cityLocation = cityLocation;
    }

}

最后是我的JSON代码:

{
"contries": [
{
"id": 1,
"name_En": "Syria",
"name_Ar": "سوريا",
"flag": "~/Images/Flags/201705252005308923_syria.png",
"visible": true,
"cities": [
{
"id": 10,
"name_En": "Damascus",
"name_Ar": "دمشق",
"description_En": "Damascus (Arabic: دمشق‎‎ Dimashq [dɪˈmaʃq], Syrian: [dɪˈmɪʃeʔ]) is the capital and likely the largest city of Syria, following the decline in population of Aleppo due to the ongoing battle for the city. It is commonly known in Syria as ash-Sham (Arabic: الشام‎‎ ash-Shām) and nicknamed as the City of Jasmine (Arabic: مدينة الياسمين‎‎ Madīnat al-Yāsmīn). In addition to being one of the oldest continuously inhabited cities in the world,[3] Damascus is a major cultural and religious centre of the Levant. The city has an estimated population of 1,711,000 as of 2009\r\n\r\nDamascus (Arabic: دمشق‎‎ Dimashq [dɪˈmaʃq], Syrian: [dɪˈmɪʃeʔ]) is the capital and likely the largest city of Syria, following the decline in population of Aleppo due to the ongoing battle for the city. It is commonly known in Syria as ash-Sham (Arabic: الشام‎‎ ash-Shām) and nicknamed as the City of Jasmine (Arabic: مدينة الياسمين‎‎ Madīnat al-Yāsmīn). In addition to being one of the oldest continuously inhabited cities in the world,[3] Damascus is a major cultural and religious centre of the Levant. The city has an estimated population of 1,711,000 as of 2009\r\n\r\n\r\nDamascus (Arabic: دمشق‎‎ Dimashq [dɪˈmaʃq], Syrian: [dɪˈmɪʃeʔ]) is the capital and likely the largest city of Syria, following the decline in population of Aleppo due to the ongoing battle for the city. It is commonly known in Syria as ash-Sham (Arabic: الشام‎‎ ash-Shām) and nicknamed as the City of Jasmine (Arabic: مدينة الياسمين‎‎ Madīnat al-Yāsmīn). In addition to being one of the oldest continuously inhabited cities in the world,[3] Damascus is a major cultural and religious centre of the Levant. The city has an estimated population of 1,711,000 as of 2009",
"description_Ar": "دمشق، هي عاصمة الجمهورية العربية السورية، ومركز محافظة دمشق.[4] وهي أحدى أقدم مدن العالم مع تاريخ غير منقطع منذ أحد عشر ألف عام تقريبًا، وأقدم مدينة - عاصمة في العالم.[5][6][7] أصبحت عاصمة منطقة سوريا منذ عام 635.[8]\r\n\r\nهناك عدة نظريات في شرح معنى اسم دمشق، أوفرها انتشارًا كون اللفظة سامية قديمة بمعنى الأرض المسقية؛[9] يعود ذلك لموقع المدينة الجغرافي في سهل خصيب يرويه نهر بردى وفروعه العديدة، مشكلاً بذلك غوطة دمشق؛ وأيضًا يتميز موقع المدينة بوجود جبل قاسيون فيها.[10][11] اقتصرت تسمية دمشق على المدينة القديمة حتى القرون الوسطى، حين أخذت المدينة بالتوسع خارجها، حاليًا تتألف دمشق من خمسة عشر منطقة سكنية متصلة بمحيطها من الضواحي لتشكل ما يعرف باسم دمشق الكبرى.",
"images": [
{
"id": 30,
"path": "~/Images/Cities/201707291821109745_dam2.jpg"
}
],
"cityLocation": "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d106464.97413085829!2d36.22864780748151!3d33.50058510608847!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x1518e6dc413cc6a7%3A0x6b9f66ebd1e394f2!2sDamascus%2C+Syria!5e0!3m2!1sen!2s!4v1492875752325"
},
{
"id": 11,
"name_En": "Latakia",
"name_Ar": "اللاذقية",
"description_En": "Latakia; Lattakia or Latakiyah (Arabic: اللَاذِقِيَّة‎‎ al-Lādhiqīyah  Syrian pronunciation: [el.laːdˈʔɪjje, -laːðˈqɪjja]), is the principal port city of Syria, as well as the capital of the Latakia Governorate. Historically, it has also been known as Lāŏdĭcḗa or Lāŏdĭcḗa ad Mắre. In addition to serving as a port, the city is a manufacturing center for surrounding agricultural towns and villages. According to the 2004 official census, the population of the city is 383,786.[3][4] It is the 5th-largest city in Syria after Aleppo, Damascus, Homs and Hama, and it borders Tartus to the south, Hama to the east, and Idlib to the north. Cape Apostolos Andreas, the north-eastern tip of Cyprus, is about 68 miles (109 km) away.[5]\r\n\r\nAlthough the site has been inhabited since the 2nd millennium BCE, the modern-day city was first founded in the 4th century BCE under the rule of the Seleucid empire. Latakia was subsequently ruled by the Romans, then the Ummayads and Abbasids in the 8th–10th centuries CE. Under their rule, the Byzantines frequently attacked the city, periodically recapturing it before losing it again to the Arabs, particularly the Fatimids. Afterward, Latakia was ruled successively by the Seljuk Turks, Crusaders, Ayyubids, Mamluks, and the Ottomans. Following World War I, Latakia was assigned to the French mandate of Syria, in which it served as the capital of the autonomous territory of the Alawites. This autonomous territory became the Alawite State in 1922, proclaiming its independence a number of times until reintegrating into Syria in 1944",
"description_Ar": "اللاذقية (باليونانية:Λαοδικεία نقحرة: لاوديكيا، باللاتينية: Laodicea ad Mare) مدينة سوريّة، تعتبر الخامسة في الجمهورية من حيث عدد السكان، بعد دمشق وحلب وحمص وحماة، تقع على الساحل الشرقي البحر الأبيض المتوسط، ضمن شبه جزيرة بحرية على بعد حوالي 385 كم من الشمال الغربي للعاصمة دمشق وهي المنفذ الأول للبلاد على البحر المتوسط والحاضنة لأكبر مرافئها، ما أكسبها موقعًا تجاريًا فريدًا، وأغناها بالعديد من المرافق الحيوية والصناعية والتجاريّة؛ فضلاً عن ذلك فإن المدينة هي المركز الإداري لمحافظة اللاذقية.\r\n\r\nأيضًا فإن المدينة تعتبر مركزًا سياحيًا هامًا لغناها بالمواقع الأثريّة التي يرقى بعضها إلى العصر الفنيقي، فضلاً عن المناخ المعتدل وتوفر خدمات الصناعة السياحية. كانت المنطقة التي تشغلها المدينة حاليًا مأهولة بالسكن البشري منذ العصر الحجري، وقد شهدت ازدهارًا فنيًا واقتصاديًا وثقافيًا نادرًا وظهرت من أوغاريت الأبحدية الأولى.[1] كما كانت مركزًا هامًا في العصرين السلوقي والروماني، إلا أن وقوعها قرب الحدود مع الإمبراطورية البيزنطيّة بعد الفتح الإسلامي، حولها لما يشبه دول الثغور، وأدى أيضًا إلى تراجع أهميتها ودورها، وما ساهم في تردي الوضع الكوارث الطبيعية والزلازل التي أصابتها، فضلاً عن الإهمال الإداري خصوصًا إبان الحكم العثماني؛ بيد أن المدينة قد أخذت أهميتها في التنامي منذ القرن العشرين، واستطاعت أن تصبح مركزًا تجاريًا وصناعيًا وثقافيًا وسياحيًا هامًا، حتى غدت مقصد حوالي نصف مليون زائر سنويًا.[2]\r\n\r\nتطور عدد سكان المدينة بشكل ملحوظ منذ بداية القرن العشرين بفضل تزايد أهميتها ونمو سوقها التجاري، ويبلغ عدد سكانها حسب إحصاءات 2009 650,558 نسمة، وهي مدينة متنوعة طائفيًا فهناك مسلمون علويون ومسلمون سنيّون والمسيحيون أغلبهم يتبع طائفة الروم الأرثوذكس إلى جانب أقليات أخرى؛ أما من الناحية العرقية فالعرب السوريون هم الأغلبية الساحقة مع وجود أقليات أرمنية وتركمانية. أغلب سكان المدينة متعلمون وتبلغ نسبة الأمية في المحافظة 9% أي أقل من المعدل العام في سوريا.[3] أما النشاط الاقتصادي للسكان فهو يبدأ من خدمات الاستيراد والتصدير ومن ثم الأعمال المرتبطة بالسياحة والصناعة حيث ينشط في المدينة عددٌ من الصناعات كالسجاد والألمنيوم والإسفلت وغيره.[4] اختيرت قلعة صلاح الدين، على بعد 3 كم من المدينة، كواحدة من مواقع التراث العالمي، المحمي من قبل اليونيسكو.[5]",
"images": [
{
"id": 8,
"path": "~/Images/Cities/201704282133483574_latt1.jpg"
},
{
"id": 9,
"path": "~/Images/Cities/201704282133483614_latt2.jpg"
},
{
"id": 10,
"path": "~/Images/Cities/201704282133483654_latt3.jpg"
}
],
"cityLocation": "https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d103885.32796422255!2d35.722152676448324!3d35.54341460064509!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x1526ac2d61d4607d%3A0x8e325bf8a14195de!2sLatakia%2C+Syria!5e0!3m2!1sen!2s!4v1493407817028"
}
]
},
{
"id": 2,
"name_En": "Lebanon",
"name_Ar": "لبنان",
"flag": "~/Images/Flags/lebanon.png",
"visible": true,
"cities": []
},
{
"id": 11,
"name_En": "France",
"name_Ar": "فرنسا",
"flag": "~/Images/Flags/201705061252346856_france.png",
"visible": true,
"cities": []
}
]
}

由于

1 个答案:

答案 0 :(得分:1)

您的JSON根目录是JsonObject,你有国家列表,所以你应该这样做,

public class ResponseValue {

    private List<Countries> contries;

    // getters 
    // setters
}

Yous这个课程得到了回应。

相关问题