retrofit-get

接口

package com.example.retrofit_gson;

/**
 * date:2018/6/12
 * author:殷成龙
 * function:黑猫警长
 */
public class Contsant {
    public static final String REG_URL="http://m2.qiushibaike.com/";

}

主页面

package com.example.retrofit_gson;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.retrofit_gson.Bean;
import com.example.retrofit_gson.Contsant;
import com.example.retrofit_gson.MyServer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

public class MainActivity extends AppCompatActivity {

    private Retrofit retrofit;
    private TextView tv_title;
    private MyServer myServer;
    private List<Bean.ItemsBean> beanList = new ArrayList<>();

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

        retrofit = new Retrofit.Builder()
                .baseUrl(Contsant.REG_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        myServer = retrofit.create(MyServer.class);

        /*myServer.getLatestJsonString().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    tv_title.setText(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });*/

        int a = 1;
        myServer.getInfoList("latest",a).enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                List<Bean.ItemsBean> items = response.body().getItems();
                beanList.addAll(items);
                tv_title.setText(beanList.get(1).getContent());
            }
            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
            }
        });
    }
}

接口类

package com.example.retrofit_gson;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;

/**
 * date:2018/6/12
 * author:殷成龙
 * function:黑猫警长
 */
public interface MyServer {

    @GET("article/list/latest?page=1")
    Call<ResponseBody> getLatestJsonString();

    @GET("article/list/{type}?")
    Call<Bean> getInfoList(@Path("type") String type, @Query("page") int page);
}

接口类

package com.example.retrofit_gson;



import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;

/**
 * date:2018/6/12
 * author:殷成龙
 * function:
 */

public interface MyServerInterface {

    //GET网络请求
    /**
     * 基本的Get请求
     * http://m2.qiushibaike.com/article/list/latest?page=6
     * RxJava和Retrofit的封装,其中有一点就是解决这个问题
     */
    @GET("article/list/latest?page=1")
    Call<ResponseBody> getLatestJsonString();

    /**
     * http://m2.qiushibaike.com/article/list/latest?page=6
     * 指定Path参数和Query参数
     * 注解中的变量{ 变量名称自定义 } 在方法参数中
     * 格式:@Path("上面定义的变量名") 类型 定义的变量名  作用:替换URL地址中{}所包含的部分
     * 格式:@Query("参数名") 类型 定义到的参数名   作用:会拼接在URL的最后部分,类似追加了"page = 1"的字符串,形成后提供给服务器的网址
     */
    @GET("article/list/{type}?")
    Call<Bean.ItemsBean.UserBean> getInfoList(@Path("type") String type, @Query("page") int page);

    /**
     * 请求网络,动态的使用网址
     * 在注解中不指定URL地址,代码中指定网址,会忽略默认网址,使用指定的网址
     */
    @GET
    Call<ResponseBody> getNetWorkDate(@Url String urlString);

    /**
     * Get请求,方法中没有参数,但是我在注解里面定义了完整URL路径,这种情况下BaseURL会被忽略掉
     */
    @GET("http://img.61gequ.com/allimg/2013-02/2167-130201101H7.jpg")
    Call<ResponseBody> getNetWorkDate();

    /**
     * Get请求提交表单的数据
     * @QueryMap 参数将在Url地址中添加 " type =text & count = 30"的字符串
     */
    @GET("web/LoginServlet")
    Call<ResponseBody> getRegInfo(@QueryMap Map<String, String> map);

}
布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.retrofit_gson.MainActivity">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

//Retrofit2的依赖
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
不要忘记加网络权限


retrofit-spring-boot-starter是一个用于整合Retrofit库和Spring Boot的starter项目,它可以简化在Spring Boot中使用Retrofit的配置和使用。 以下是retrofit-spring-boot-starter的使用方法: 1. 在你的Spring Boot项目的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.lianjiatech</groupId> <artifactId>retrofit-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> ``` 2. 创建一个接口,用于定义Retrofit的API接口。例如: ```java import retrofit2.http.GET; import retrofit2.http.Path; public interface MyApi { @GET("/users/{username}") User getUser(@Path("username") String username); } ``` 3. 在你的Spring Boot应用程序中,使用`@Autowired`注解将Retrofit的API接口注入到你的代码中。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import retrofit2.Retrofit; @Service public class MyService { private final MyApi myApi; @Autowired public MyService(Retrofit retrofit) { this.myApi = retrofit.create(MyApi.class); } public User getUser(String username) { return myApi.getUser(username); } } ``` 4. 现在你可以在你的代码中使用`MyService`来调用Retrofit的API接口了。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { private final MyService myService; @Autowired public MyController(MyService myService) { this.myService = myService; } @GetMapping("/users/{username}") public User getUser(@PathVariable String username) { return myService.getUser(username); } } ``` 以上是retrofit-spring-boot-starter的基本用法。你可以根据自己的需求进行配置和使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值