一.框架使用基本步骤
1.首先是导入依赖,添加网络权限
implementation 'com.squareup.retrofit:retrofit:2.0.0-beta2'
implementation 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
2.创建retrofit对象并初始化
3.分析url得到baseurl(必须以‘/’结尾)创建接口并在接口中以@GET等方式获得url其他的拼接参数,接口返回值是一个Call类型的回调接口
二.使用Retrofit获取网站内容(以每日一文为例)
url:https://interface.meiriyiwen.com/article/today?dev=1
因为报了“cannot resolve symbol 'ScalarsConverterFactory'”,我又修改了依赖:
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation'com.squareup.retrofit2:converter-scalars:2.3.0'
package com.example.lenovo.retrofitdemo;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**配置接口,@GET注明相对地址,@Query注明参数*/
public interface ApiService {
@GET("today")
Call get(@Query("dev")int dev);
}
package com.example.lenovo.retrofitdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**baseUrl里面的内容要记得用“”括起来,如果确定服务器的返回类型是固定的,
* 可以使用StringConvertFactory方法
* 我在写到这里的时候发现ScalarsConverterFactory方法所用依赖没有被成功导入,
* 我又在Gradle里添加“compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'
”,结果大抵是和之前导入的其他依赖冲突了,retrofit和okhttp报红,我又重新调整了gradle*/
Retrofit retrofit=new Retrofit.Builder().
baseUrl("https://interface.meiriyiwen.com/article/").
addConverterFactory(ScalarsConverterFactory.create()).
client(new OkHttpClient()).
build();
/**实例化之后,创建一个接口的实例*/
ApiService query=retrofit.create(ApiService.class);
//创建请求,传入参数
Call call=query.get(1);
//执行异步网络请求
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.d("mainactivity","OnResponse:"+response.body());
}
@Override
public void onFailure(Call call, Throwable t) {
Log.d("mainactivity","err:"+t);
}
});
}
}
可能我真的是bug体质,点了一下运行:没有adb调试设备,我按照网上一篇博客进行了如下命令操作:
adb shell
adb server version (31) doesn't match this client (40); killing...
error: cannot open 'C:\WINDOWS\Temp\adb.log': Permission denied
could not read ok from ADB Server
* failed to start daemon
error: cannot connect to daemon
adb shell
adb server version (31) doesn't match this client (40); killing...
error: cannot open 'C:\WINDOWS\Temp\adb.log': Permission denied
could not read ok from ADB Server
* failed to start daemon
error: cannot connect to daemon
adb nodaemon server
adb I 01-25 12:03:37 21120 4440 auth.cpp:421] adb_auth_init...
adb I 01-25 12:03:37 21120 4440 auth.cpp:174] read_key_file 'C:\Users\lenovo\.android\adbkey'...
adb I 01-25 12:03:37 21120 21076 transport.cpp:103] D3H7N18503001094: read thread spawning
adb I 01-25 12:03:37 21120 16888 transport.cpp:116] D3H7N18503001094: write thread spawning
adb I 01-25 12:03:37 21120 4440 auth.cpp:472] Calling send_auth_response
adb I 01-25 12:03:37 21120 4440 adb.cpp:145] D3H7N18503001094: offline
然后再点运行,我的设备显示出来了,运行后logcat里面报错
java.lang.IllegalArgumentException: Could not locate ResponseBody converter for class com.example.lenovo.retrofitdemo.Bean.出错了没别的办法,只能改呗。分析这个问题,我觉得应该是baseurl和参数设置不正确造成的。
改动的地方:
baseUrl("https://interface.meiriyiwen.com/").
addConverterFactory(GsonConverterFactory.create()).
@GET("article/today?dev=1")
Call getData();
implementation'com.squareup.retrofit2:converter-gson:2.0.2'
这样改动是因为
为了看着清晰点,我设置了个按钮,然后把请求另写了个方法放进去了。
package com.example.lenovo.retrofitdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_get).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getRequest();
}
});
}
/**为get请求写一个方法*/
private void getRequest()
{
/**baseUrl里面的内容要记得用“”括起来,如果确定服务器的返回类型是固定的,
* 可以使用StringConvertFactory方法
* 我在写到这里的时候发现ScalarsConverterFactory方法所用依赖没有被成功导入,
* 我又在Gradle里添加“compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'
”,结果大抵是和之前导入的其他依赖冲突了,retrofit和okhttp报红,我又重新调整了gradle*/
Retrofit retrofit=new Retrofit.Builder().
baseUrl("https://interface.meiriyiwen.com/").
addConverterFactory(GsonConverterFactory.create()).
client(new OkHttpClient()).
build();
/**实例化之后,创建一个接口的实例*/
ApiService query=retrofit.create(ApiService.class);
//创建请求,传入参数
Call call=query.getData();
//执行异步网络请求
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Log.d("mainactivity","OnResponse:"+response.body());
}
@Override
public void onFailure(Call call, Throwable t) {
Log.d("mainactivity","err:"+t);
}
});
}
}
我又改用Map传键值对的方法传(“dev”,1),改了返回的结果还是null.等我找到解决方法了我会把这篇博客补完整。