Retrofit2学习项目_2

本文介绍如何在Android应用中使用Retrofit库进行网络请求。包括添加依赖、配置权限、创建UI界面、定义数据模型、设置API接口及实现请求响应等步骤。

1.Ctrl+Alt+Shift+S 打开ProjectStructure ->app->Dependencies
添加:

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

2.添加网络权限

<uses-permission android:name="android.permission.INTERNET"/>

3.修改activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.rokkki.retrofit_2.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello"/>
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"/>

</LinearLayout>

4.添加Book.java类:
API地址:
https://api.douban.com/v2/book/search?q=%E9%87%91%E7%93%B6%E6%A2%85&tag=&start=0&count=1
同样用GsonFormat方式,之后手动添加toString函数

5.Service.java:

public interface Service {
    @GET("book/search")
    Call<Book> getBook(@Query("q") String name,
                       @Query("tag") String tag,
                       @Query("start") int start,
                       @Query("count") int count);
}

6.修改MainActivity.java:

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;
    public static final String  API_URL="https://api.douban.com/v2/";

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

        textView = (TextView)findViewById(R.id.text_view);
        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(API_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                Service service = retrofit.create(Service.class);

                Call<Book> call = service.getBook("金瓶梅",null,0,1);
                call.enqueue(new Callback<Book>() {
                    @Override
                    public void onResponse(Call<Book> call, Response<Book> response) {
                        textView.setText(response.body().toString());
                    }

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

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值