android开发练习二-Retrofit

 http://blog.csdn.net/biezhihua/article/details/49232289#comments  找了很久,只有这里介绍的非常详细,果断把代码拉下来试试看

 

Error:Execution failed for task ':app:transformClassesWithInstantRunVerifierForDebug'.
> java.io.FileNotFoundException: D:\workspace\MyApplication2\app\build\intermediates\classes\debug\com\example\administrator\myapplication\retrofit (拒绝访问。)

第一次运行时出现这个错误,看了半天没发现什么问题于是重新运行

FATAL EXCEPTION: main
                                                                                       Process: com.example.administrator.myapplication, PID: 1147
                                                                                       java.lang.IllegalStateException: Could not execute method for android:onClick

似乎说是主线程不能运行,用断点debug看一下。。

果然是主线程不能进行网络访问,看来这个方法默认是同步运行的

Response<List<Contributor>> response = call.execute(); // 同步
然后异步运行测试一下
Call<List<Contributor>> call1 = call.clone();// 5. 请求网络,异步call1.enqueue(new Callback<List<Contributor>>() {    @Override    public void onResponse(Response<List<Contributor>> response, Retrofit retrofit) {        Log.d(TAG, "response:" + response.body().toString());    }    @Override    public void onFailure(Throwable t) {    }});
还是报错了
FATAL EXCEPTION: main
                                                                                       Process: com.example.administrator.myapplication, PID: 3392
                                                                                       java.lang.IncompatibleClassChangeError: Class 'com.example.administrator.myapplication.MainActivity$1' does not implement interface 'retrofit.Callback' in call to 'void retrofit.Callback.onResponse(retrofit.Response, retrofit.Retrofit)' (declaration of 'retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1' appears in /data/app/com.example.administrator.myapplication-1/base.apk)

把call1改成call,仍然报错

reportError [type: 211, code: 524300]: Error at processing input stream
02-25 13:20:56.811 27931-28078/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
                                                                                                com.google.android.apps.gsa.shared.speech.a.g: Error at processing input stream
加断点debug查看一下。追踪中发现url地址合成全过程。。 https://api.github.com/repos/square/retrofit/contributors
追踪完毕发现大约是在okhttp那里断掉的,

okhttp内部依赖okio,导入okio:gradle: compile 'com.squareup.okio:okio:1.5.0'试试看

成功获取response!

mytag: response:[Contributor{login='JakeWharton', contributions=720}, Contributor{login='swankjesse', contributions=142}, Contributor{login='pforhan', contributions=48}, Contributor{login='eburke', contributions=36}, Contributor{login='dnkoutso', contributions=26}, Contributor{login='edenman', contributions=24}, Contributor{login='loganj', contributions=17}, Contributor{login='rcdickerson', contributions=14}, Contributor{login='rjrjr', contributions=13}, Contributor{login='kryali', contributions=9}, Contributor{login='holmes', contributions=8}, Contributor{login='adriancole', contributions=8}, Contributor{login='swanson', contributions=7}, Contributor{login='crazybob', contributions=6}, Contributor{login='JayNewstrom', contributions=5}, Contributor{login='Turbo87', contributions=5}, Contributor{login='danrice-square', contributions=5}, Contributor{login='ransombriggs', contributions=4}, Contributor{login='koalahamlet', contributions=3}, Contributor{login='codebutler', contributions=3}, Contributor{login='artem-zinnatullin', contributions=3}, Contributor{login='icastell', contributions=3}, Contributor{login='jjNford', contributions=3}, Contributor{login='f2prateek', contributions=3}, Contributor{login='NightlyNexus', contributions=3}, Contributor{login='benoitdion', contributions=2}, Contributor{login='xian', contributions=2}, Contributor{login='dlew', contributions=2}, Contributor{login='kaiwaldron', contributions=2}, Contributor{login='larsgrefer', contributions=2}]

之后改写response方法试图直接更新ui

public void onResponse(Response<List<Contributor>> response, Retrofit retrofit) {
    String a= response.body().get(0).login;
    int b=response.body().get(0).contributions;
     temperature.setText(a);
    humidity.setText(b+"");
}
成功更新,证明onResponse方法运行在主线程上。下一步用Retrofit改写我的天气程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值