之前返回的是对象,但是我想看看返回的字符串。
之前的代码:
package com.example.z.study;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.imageView)
ImageView imageView;
Animation animation;
@BindView(R.id.tv_rx)
TextView tvRx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
ButterKnife.bind(this);
animation = AnimationUtils.loadAnimation(this, R.anim.demo);
}
@OnClick(R.id.btn_anim)
public void setBtnAnim() {
imageView.startAnimation(animation);
}
@OnClick(R.id.btn_rx)
public void btn_rx() {
getString();
}
public void getString() {
String baseUrl = "https://api.douban.com/v2/movie/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())//转为对象类型
.build();
MovieService movieService = retrofit.create(MovieService.class);
Call<MovieVo> call = movieService.getTopMovie(0, 10);
call.enqueue(new Callback<MovieVo>() {
@Override
public void onResponse(Call<MovieVo> call, Response<MovieVo> response) {
tvRx.setText(response.body().toString());
System.out.println("**********" + response.body());
Gson gson = new Gson();
}
@Override
public void onFailure(Call<MovieVo> call, Throwable throwable) {
System.out.println("--------------------");
tvRx.setText(throwable.getMessage());
}
});
}
public interface MovieService {
@GET("top250")
Call<MovieVo> getTopMovie(@Query("start") int start, @Query("count") int count);
}
}
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.z.study"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
// // https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava
// compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.0'
https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxandroid
// compile group: 'io.reactivex.rxjava2', name: 'rxandroid', version: '2.0.1'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit
compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.3.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson
compile group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.3.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/adapter-rxjava
compile group: 'com.squareup.retrofit2', name: 'adapter-rxjava', version: '2.3.0'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'
// https://mvnrepository.com/artifact/com.jakewharton/butterknife
compile group: 'com.jakewharton', name: 'butterknife', version: '8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}
现在的代码:
package com.example.z.study;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.imageView)
ImageView imageView;
Animation animation;
@BindView(R.id.tv_rx)
TextView tvRx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
ButterKnife.bind(this);
animation = AnimationUtils.loadAnimation(this, R.anim.demo);
}
@OnClick(R.id.btn_anim)
public void setBtnAnim() {
imageView.startAnimation(animation);
}
@OnClick(R.id.btn_rx)
public void btn_rx() {
getString();
}
public void getString() {
String baseUrl = "https://api.douban.com/v2/movie/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
MovieService movieService = retrofit.create(MovieService.class);
Call<String> call = movieService.getTopMovie(0, 10);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
tvRx.setText(response.body().toString());
System.out.println("**********" + response.body());
Gson gson = new Gson();
}
@Override
public void onFailure(Call<String> call, Throwable throwable) {
System.out.println("--------------------");
tvRx.setText(throwable.getMessage());
}
});
}
public interface MovieService {
@GET("top250")
Call<String> getTopMovie(@Query("start") int start, @Query("count") int count);
}
}
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.z.study"
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
testCompile 'junit:junit:4.12'
// // https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxjava
// compile group: 'io.reactivex.rxjava2', name: 'rxjava', version: '2.1.0'
https://mvnrepository.com/artifact/io.reactivex.rxjava2/rxandroid
// compile group: 'io.reactivex.rxjava2', name: 'rxandroid', version: '2.0.1'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit
compile group: 'com.squareup.retrofit2', name: 'retrofit', version: '2.3.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-gson
compile group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.3.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-scalars
compile group: 'com.squareup.retrofit2', name: 'converter-scalars', version: '2.3.0'
// https://mvnrepository.com/artifact/com.squareup.retrofit2/adapter-rxjava
compile group: 'com.squareup.retrofit2', name: 'adapter-rxjava', version: '2.3.0'
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'
// https://mvnrepository.com/artifact/com.jakewharton/butterknife
compile group: 'com.jakewharton', name: 'butterknife', version: '8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}
关键代码:
.addConverterFactory(GsonConverterFactory.create())//转为对象类型
compile group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.3.0'
.addConverterFactory(ScalarsConverterFactory.create())
compile group: 'com.squareup.retrofit2', name: 'converter-scalars', version: '2.3.0'