Retrofit简单的实例

这篇博客面向的对象是Retrofit新手,如果是老手麻烦帮忙看一下代码时候有可以修改得更好点,谢谢了!

对于新手来说,看一大篇文章之后,急需一个小小的项目来练手,然而网上的例子的代码有点复杂,所以将我自己弄的小例子供新手学习,大家一起互相学习!(直接复制粘贴可用)

0准备工作

Android Stuido中下载插件GsonFormat,因为后面需要生成实体类。


AndroidManifest.xml

 


app.gradle中加入

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'



1项目的框架


Retrofit简单的实例_第1张图片

2bean


bean里主要放的是接收来的JSON实体对象。使用GsonFormat自动生成的,具体方法度娘一下就出来了。

public class Bean {
    /**
     * count : 3282
     * description : 尾鳍分叉颇深,下叶较长
     * disease :
     * fcount : 0
     * food :
     * id : 10
     * img : /food/150804/285158e45d996bbd47046ee2af2c7370.jpg
     * keywords : 青灰色 银白色 甲壳类 成鱼 菱形
     * message : 鲳鱼属于鲈形目,鲳科。体短而高,极侧扁,略呈菱形。头较小,吻圆,口小,牙细。成鱼腹鳍消失。尾鳍分叉颇深,下叶较长。体银白色,上部微呈青灰色。以甲壳类等为食。
     * name : 白鲳鱼
     * rcount : 0
     * status : true
     * summary :
     * symptom :
     * url : http://www.tngou.net/food/show/10
     */

    private int count;
    private String description;
    private String disease;
    private int fcount;
    private String food;
    private int id;
    private String img;
    private String keywords;
    private String message;
    private String name;
    private int rcount;
    private boolean status;
    private String summary;
    private String symptom;
    private String url;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDisease() {
        return disease;
    }

    public void setDisease(String disease) {
        this.disease = disease;
    }

    public int getFcount() {
        return fcount;
    }

    public void setFcount(int fcount) {
        this.fcount = fcount;
    }

    public String getFood() {
        return food;
    }

    public void setFood(String food) {
        this.food = food;
    }

    public int getId() {
        return id;
    }

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

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getName() {
        return name;
    }

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

    public int getRcount() {
        return rcount;
    }

    public void setRcount(int rcount) {
        this.rcount = rcount;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getSymptom() {
        return symptom;
    }

    public void setSymptom(String symptom) {
        this.symptom = symptom;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

3retrofit

IBeanService

public interface IBeanService {
    @GET("show")
    Call getMenuById(@Query("id") String id);
}
@GET内的参数,是跟在baseURL后面的,所以该URL的具体代码是:
http://www.tngou.net/api/food/ show?id=

Retrofit

public class Retrofit {
    private IBeanService service;

    /**
     * 获取Retrofit实例
     * @return
     */
    public static Retrofit getRetrofit(){
        return new Retrofit();
    }

    private Retrofit() {
        retrofit2.Retrofit retrofit = new retrofit2.Retrofit.Builder()
                .baseUrl("http://www.tngou.net/api/food/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
         service = retrofit.create(IBeanService.class);
    }

    /**
     * 获取IBeanService实例
     * @return
     */
    public IBeanService getService(){
        return service;
    }
}

4MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText mEditText;
    private TextView mTextView;
    private Button mButton;

    private IBeanService service;
    private Retrofit retrofit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText) findViewById(R.id.et_input);
        mTextView = (TextView) findViewById(R.id.tv_2);
        mButton = (Button) findViewById(R.id.bt_query);

        retrofit=Retrofit.getRetrofit();
        service= retrofit.getService();

        mButton.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_query:
                query();
                break;
        }
    }

    private void query(){
        mTextView.setText("");
        String num=mEditText.getText().toString();

        if(num.isEmpty()){
            Toast.makeText(MainActivity.this, "请输入ID", Toast.LENGTH_SHORT).show();
            return;
        }


        Call call = service.getMenuById(num);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                if (response.isSuccessful()) {
                    Bean result = response.body();//关键
                    if (result != null) {
                        String msg = result.getMessage();
                        String name=result.getName();
                        mTextView.setText("message="+msg+"name="+name);
                    }
                }
            }

            @Override
            public void onFailure(Call call, Throwable t) {

            }
        });
    }
}

5activity_main.xml

    
    
    


大家相互学习,有问题或者建议的,请留言,谢谢了!


你可能感兴趣的:(Android)