2018-01-27
原CSDN博客已弃用,文章会逐渐迁移过来。
一、使用Retrofit框架请求网页
一、添加依赖
请见
https://blog.csdn.net/weimeig/article/details/78656178
二、建立接口文件
Service.java
package com.administrator.picassodemo;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* Created by Administrator on 2018/5/16.
*/
public interface Service {
@GET("/")//括号内可以不写,也可以写网页的子目录
Call<String> getBaidu();
}
三、创建实例
MainActivity.java
package com.administrator.picassodemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity implements Callback<String> {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.tvContent);
//addConverterFactory()解析下载数据
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://qq.com")
.addConverterFactory(new Converter.Factory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
return value.string();
}
};
}
}).build();
//用实例出来一个Service
Service service = retrofit.create(Service.class);
//用直接get出来一个请求
Call<String> call = service.getBaidu();
// 异步请求
call.enqueue(this);
}
// ------得到两个反馈------
@Override
public void onResponse(Call<String> call, Response<String> response) {
mTextView.setText(response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(this,"请求失败"+call.request().url(),Toast.LENGTH_SHORT).show();
t.printStackTrace();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.administrator.picassodemo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
二、get请求和数据解析
(一)、添加依赖
dependencies {
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.administrator.picassodemo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
数据实体类
Cook.java
package com.administrator.picassodemo;
import com.google.gson.annotations.SerializedName;
/**
* Created by Administrator on 2018/5/16.
*/
public class Cook {
@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
@SerializedName("food")
private String food;
@SerializedName("img")
private String img;
@SerializedName("images")
private String images;
@SerializedName("description")
private String description;
@SerializedName("keywords")
private String keywords;
@SerializedName("message")
private String message;
@SerializedName("count")
private int count;
@SerializedName("fcount")
private int fcount;
@SerializedName("rcount")
private int rcount;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
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 int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getFcount() {
return fcount;
}
public void setFcount(int fcount) {
this.fcount = fcount;
}
public int getRcount() {
return rcount;
}
public void setRcount(int rcount) {
this.rcount = rcount;
}
}
返回的数据有集合数据,所以建立集合数据实体类
Tngou.java
package com.administrator.picassodemo;
import com.google.gson.annotations.SerializedName;
import java.util.List;
/**
* Created by Administrator on 2018/5/16.
*/
public class Tngou {
@SerializedName("status")
private boolean status;
@SerializedName("total")
private int total;
@SerializedName("tngou")
private List<Cook> list;
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public List<Cook> getList() {
return list;
}
public void setList(List<Cook> list) {
this.list = list;
}
}
建立接口文件
Service.java
package com.administrator.picassodemo;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* Created by Administrator on 2018/5/16.
*/
public interface Service {
@GET("/api/cook/list")//括号内可以不写,也可以写网页的子目录
Call<Tngou> getList(@Query("id") int id, @Query("page") int page,@Query("rows") int rows);
}
MainActivity.java
package com.administrator.picassodemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.GsonConverterFactory;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity implements Callback<Tngou> {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.tvContent);
//addConverterFactory()解析下载数据
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.tngou.net")
.addConverterFactory(GsonConverterFactory.create())
.build();
Service service = retrofit.create(Service.class);
Call<Tngou> call = service.getList(0,1,20);
call.enqueue(this);
}
@Override
public void onResponse(Call<Tngou> call, Response<Tngou> response) {
List<Cook> list = response.body().getList();
}
@Override
public void onFailure(Call<Tngou> call, Throwable t) {
t.printStackTrace();
}
}