retrofit的应用

导入
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
======主方法======
package com.example.admin.retrofittest2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

public class MainActivity extends AppCompatActivity {
private String path="http://api.expoon.com/AppNews/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(path)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService apiService=retrofit.create(ApiService.class);
        Call<Interfacetest> call= apiService.getbean();
        call.enqueue(new Callback<Interfacetest>() {
            @Override
            public void onResponse(Response<Interfacetest> response, Retrofit retrofit) {
                Interfacetest body = response.body();
                Log.d("返回回来的数据:", body.getInfo());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("返回回来的数据:", "onFailure");
            }
        });
    }
}
===========定义一个接口=============
package com.example.admin.retrofittest2;


import retrofit.Call;
import retrofit.http.GET;

/**
 * Created by admin on 2016/8/24.
 */
public interface ApiService {
    @GET("getNewsList/type/1/p/1")
    Call<Interfacetest> getbean();
}
============写一个实体类==================


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值