//依赖 权限
//compile 'com.squareup.retrofit2:retrofit:2.0.0'
//compile 'com.squareup.retrofit2:converter-gson:2.0.2'
//Main
package com.example.myretrofit;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class MainActivity extends AppCompatActivity {
private TextView ll;
private Call<ResponseBody> call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView qs = (TextView) findViewById(R.id.qs);
Button viewById = (Button) findViewById(R.id.xuanfu);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Main3Activity.class);
startActivity(intent);
}
});
qs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);
}
});
ll = (TextView) findViewById(R.id.ll);
initReteofit();
}
private void initReteofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.URL_BASE)
.build();
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
call = retrofitService.getLatestJsonString();
// 用法和OkHttp的call如出一辙,
// 不同的是如果是Android系统回调方法执行在主线程
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//对返回结果做成功与内容的判断
if (response.isSuccessful() && response.body() != null) {
try {
//得到结果
String string = response.body().string();
Log.e("chen", "onResponse: "+string );
//更新UI
ll.setText(string);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
// t.printStackTrace();
Log.e("chen", "onFailure: "+"加载失败" );
}
});
}
//为节约资源,当屏幕不可见时,我们停止网络请求
@Override
protected void onStop() {
super.onStop();
if (call != null) {
call.cancel();
}
}
}
//main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.myretrofit.MainActivity">
<TextView
android:text="dxjfbv"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="@+id/ll"/>
<Button
android:id="@+id/qs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击我给你看糗事段子"/>
<Button
android:id="@+id/xuanfu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到炫富列车"/>
</LinearLayout>
package com.example.myretrofit;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
public interface RetrofitService {
//请求方式为GET,参数为basil2style,因为没有变量所以下面getString方法也不需要参数
@GET("article/list/latest?page=1")
//定义返回的方法,返回的响应体使用了ResponseBody
Call<ResponseBody> getLatestJsonString();
@POST("")
Call<ResponseBody> chen();
}