1

我只是在实施Retrofit Library-2.0.1,但我得到的是NULL数据,如下图所示。

在此处输入图像描述

我正在尝试使用简单的 GET 方法从 API 加载所有类别,如下所示...

http://dev.punchprint.com.au/webservices/WebService.php?method=listCategory

以下是浏览器响应...

{
  "error": "0",
  "success": "1",
  "message": "List Category",
  "data": [
    {
      "id": "3",
      "name": "Pendants",
      "link_rewrite": "pendants",
      "image": "http://punchprint.com.au/c/3-category_default/pendants.jpg",
      "products": [
        "1",
        "8",
        "9",
        "11",
        "12",
        "13",
        "14",
        "37",
        "42",
        "79",
        "80"
      ]
    },
    {
      "id": "7",
      "name": "Key Rings",
      "link_rewrite": "key-rings",
      "image": "http://punchprint.com.au/c/7-category_default/key-rings.jpg",
      "products": [
        "21",
        "37",
        "47",
        "51"
      ]
    },
    {
      "id": "8",
      "name": "Personalised Phone Cover",
      "link_rewrite": "personalised-phone-cover",
      "image": "http://punchprint.com.au/c/8-category_default/personalised-phone-cover.jpg",
      "products": [
        "40",
        "41",
        "54",
        "55",
        "57",
        "58",
        "59",
        "61"
      ]
    },
    {
      "id": "14",
      "name": "Personalised Crystal & Rock",
      "link_rewrite": "personalised-crystal-rock",
      "image": "",
      "products": [
        "52",
        "53",
        "56",
        "67",
        "71",
        "72",
        "73",
        "74",
        "75",
        "76",
        "77",
        "78"
      ]
    },
    {
      "id": "6",
      "name": "Gold Pendants",
      "link_rewrite": "gold-pendants",
      "image": "http://punchprint.com.au/c/6-category_default/gold-pendants.jpg",
      "products": [
        "31",
        "32",
        "33"
      ]
    },
    {
      "id": "15",
      "name": "Engraved crystal",
      "link_rewrite": "engraved-crystal",
      "image": "http://punchprint.com.au/c/15-category_default/engraved-crystal.jpg",
      "products": [
        "62",
        "63",
        "64",
        "65",
        "66",
        "68"
      ]
    },
    {
      "id": "10",
      "name": "Gift Ideas",
      "link_rewrite": "gift-ideas",
      "image": "http://punchprint.com.au/c/10-category_default/gift-ideas.jpg",
      "products": [
        "23",
        "25",
        "26",
        "43",
        "45",
        "46",
        "48",
        "50",
        "52",
        "67",
        "68"
      ]
    },
    {
      "id": "13",
      "name": "Kid's Special",
      "link_rewrite": "kid-s-special",
      "image": "",
      "products": [
        "48"
      ]
    },
    {
      "id": "9",
      "name": "Zippo",
      "link_rewrite": "zippo",
      "image": "",
      "products": [
        "27"
      ]
    },
    {
      "id": "12",
      "name": "Pet Tags",
      "link_rewrite": "pet-tags",
      "image": "http://punchprint.com.au/c/12-category_default/pet-tags.jpg",
      "products": [
        "22",
        "37"
      ]
    },
    {
      "id": "16",
      "name": "Rock Photo",
      "link_rewrite": "rock-photo",
      "image": "http://punchprint.com.au/c/16-category_default/rock-photo.jpg",
      "products": [
        "67",
        "70"
      ]
    }
  ]
}

我尝试过的如下...

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.lazy_loading.android01.lazyloading"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
}

MainActivity.java

package com.lazy_loading.android01.lazyloading;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {

    //Default variables
    final static String BASE_URL = "http://dev.punchprint.com.au/webservices/WebService.php/";

    //Widgets
    private ListView category_list;
    private TextView tv_id, tv_name;
    private ProgressDialog progressDialog;

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

        init();
    }

    void init() {
        find_view_by_id();
        initializeElements();
        CommonMethods.showProgressDialog(progressDialog, "Loading...please wait!!");
        loadCategories();
    }

    void find_view_by_id() {
        //category_list = (ListView) findViewById(R.id.category_list);
        tv_id = (TextView) findViewById(R.id.id1);
        tv_name = (TextView) findViewById(R.id.name1);
    }

    void initializeElements() {
        progressDialog = new ProgressDialog(this);
    }

    void loadCategories() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiEndPointInterface apiInterface = retrofit.create(ApiEndPointInterface.class);

        final Call<Categories> categoriesCall = apiInterface.getCategories();

        categoriesCall.enqueue(new Callback<Categories>() {
            @Override
            public void onResponse(Call<Categories> call, Response<Categories> response) {
                Log.e("response: ", "response:-->" + response.toString());
                if (response.isSuccessful()) {
                    CommonMethods.dismissProgressDialog(progressDialog);
                    String id = response.body().getId();
                    Log.e("id: ", "id:-->" + response.body().getId());
                    String name = response.body().getName();
                    tv_id.setText(id);
                    tv_name.setText(name);
                }
            }

            @Override
            public void onFailure(Call<Categories> call, Throwable t) {
                //Log.e("Error: ", "Error:-->" + t.toString());
            }
        });
    }
}

ApiEndPointInterface.java

package com.lazy_loading.android01.lazyloading;

import retrofit2.Call;
import retrofit2.http.GET;

/**
 * Created by android01 on 08-04-2016.
 */
public interface ApiEndPointInterface {
    @GET("?method=listCategory")
    Call<Categories> getCategories();
}

模型类如下...

分类.java

package com.lazy_loading.android01.lazyloading;

/**
 * Created by android01 on 08-04-2016.
 */
public class Categories {

    /**
     * id : 3
     * name : Pendants
     * link_rewrite : pendants
     * image : http://punchprint.com.au/c/3-category_default/pendants.jpg
     * products : ["1","8","9","11","12","13","14","37","42","79","80"]
     */

    private String id;
    private String name;
    private String image;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

我也尝试过 MainActivity 中的以下代码,但它不起作用,它也给了我数据。

Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Response response = categoriesCall.execute();
                    Categories categories = (Categories) response.body();
                    Log.e("Category: ", "Category:-->" + categories.toString());
                    Log.e("Id: ", "Id:-->" + categories.getId());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();

任何帮助将非常明显,在此先感谢。

4

1 回答 1

1

你的模型不正确。它应该是这样的:

public class Categories {
   @SerializedName("error")
   String error;

   @SerializedName("success")
   String success;

   @SerializedName("message")
   String message;

   @SerializedName("data")
   ArrayList<Data> data;

   public static class Data {

      @SerializedName("id")
      String id;

      @SerializedName("name")
      String name;

      @SerializedName("link_rewrite")
      String link;

      @SerializedName("image")
      String image;

      @SerializedName("products")
      ArrayList<String> products;
   }
}

现在生成了他们的吸气剂。

于 2016-04-08T10:30:39.507 回答