Retrofit使用json

时间:2017-01-10 19:10:11

标签: json textview retrofit retrofit2

我正在尝试改造库以在我的布局中集成配置文件json,但是当我尝试将数据设置为我的文本视图时,它没有返回任何内容。

接口

public interface Item_Testing {
    @GET("datingconvay/api.php?action=users&facebook_id=368008520231015")
    Call<Recycler_Adapter> responserecycler();

    @GET("datingconvay/api.php?action=profile&facebook_id=368008520231016")
    Call<Single_Profile> singleprofile();
}

SingleProfile.java

public class Single_Profile {
    @SerializedName("profile")
    JSONObject profile;
    @SerializedName("username")
    String username;
    @SerializedName("country")
    String country;
    @SerializedName("religion")
    String religion;
    @SerializedName("nationality")
    String nationality;

}

ProfileView.java

public class Icon_Test extends AppCompatActivity {
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.ctry)
    TextView ctry;
    @BindView(R.id.religion)
    TextView religion;
    @BindView(R.id.national)
    TextView national;
    String log = "main";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.icon_test);
        ButterKnife.bind(this);
        Retrofit singfit = new Retrofit.Builder().baseUrl("http://sampletemplates.co.in/").addConverterFactory(GsonConverterFactory.create()).build();
        Item_Testing singitem=singfit.create(Item_Testing.class);
        Call<Single_Profile> callsingle=singitem.singleprofile();
        callsingle.enqueue(new Callback<Single_Profile>() {
            @Override
            public void onResponse(Call<Single_Profile> call, Response<Single_Profile> response) {
                Toast.makeText(Icon_Test.this,"This method is called", Toast.LENGTH_SHORT).show();
                Single_Profile singprofile=response.body();
                username.setText(singprofile.username);
            }

            @Override
            public void onFailure(Call<Single_Profile> call, Throwable t) {
                Log.d(log,t.toString());
            }
        });


    }
}

MyJSONData

{
    "profile": {
        "id": "4",
        "facebook_id": "368008520231016",
        "username": "Raja",
        "country": "India",
        "religion": "Hindu",
        "nationality": "Indian",
        "gender": "Male",
        "dob": "1989-06-12",
        "email": "sampletestingteam@gmail.com",
        "profile_pic": [
            "http://sampletemplates.net.in/datingconvay/uploads/keerthi2.jpg"
        ],
        "device_name": "ios",
        "device_token": "",
        "villege": "Kakinada",
        "ifeel": "",
        "description": "",
        "lat": "0",
        "lng": "0",
        "platform": ""
    },
    "status": "Success"
}

1 个答案:

答案 0 :(得分:1)

在界面中将Single_Profile更改为字符串,然后您可以打印响应

    @GET("datingconvay/api.php?action=profile&facebook_id=368008520231016")
        Call<String> singleprofile();

callsingle.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Log.d("Response",response.body);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.d(log,t.toString());
            }
        });