我是retrofit
的新手,尽管导入了适当的导入,依赖项,但仍需要通过使用retrofit
来调用JSON API。适配器接口已编译。预先感谢!
以下主要活动中有3个错误-
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
和
.subscribe(new Observer<ResponseBody>()
{
@Override
public void onCompleted() {
这是MainActivity.class
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.Observer;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.ResponseBody;
public class MainActivity extends AppCompatActivity {
public static final String BASE_URL = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt/";
private RecyclerView mRecyclerView;
String responseString;
private Adapter mAdapter;
private ArrayList<Worldpopulation> List;
JSONObject jsonObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadJSON();
}
private void loadJSON() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
APInterface apiService = retrofit.create(APInterface.class);
apiService.getFile(BASE_URL)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ResponseBody>()
{
@Override
public void onCompleted() {
Log.d("COMPLETE", "COMPLETED TRANSFER");
}
@Override
public void onError(Throwable e)
{
Log.d("ERROR", e.getMessage());
}
@Override
public void onNext(ResponseBody responseBody) {
try {
responseString = responseBody.string();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("SHIZ", responseString);
try {
jsonObject = new JSONObject(responseString);
Log.d("SHIZ", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
}
List = new ArrayList<>();
//Getting out the world population array
JSONArray jsonArray = jsonObject.optJSONArray("worldpopulation");
for (int i = 0; i < jsonArray.length(); i++) {
Worldpopulation world = new Worldpopulation();
JSONObject jsonobject = null;
try {
jsonobject = jsonArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
String rank = jsonobject.getString("rank");
world.setRank(Integer.valueOf(rank));
} catch (JSONException e) {
e.printStackTrace();
}
try {
String country = jsonobject.getString("country");
world.setCountry(country);
} catch (JSONException e) {
e.printStackTrace();
}
try {
String population = jsonobject.getString("population");
Long l = Long.parseLong(population.replaceAll(",", ""));
world.setPopulation(l);
} catch (JSONException e) {
e.printStackTrace();
}
try {
String flag = jsonobject.getString("flag");
world.setFlag(flag);
} catch (JSONException e) {
e.printStackTrace();
}
//adding data to List
List.add(world);
Log.d("COUNTRY MODEL", List.toString());
}
//Setting Adapter for RecyclerView
Adapter recyclerAdapter = new Adapter(List);
RecyclerView.LayoutManager layout = new LinearLayoutManager(MainActivity.this);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(layout);
mRecyclerView.setAdapter(recyclerAdapter);
}
});
}
}
这是代码中的编译器错误-
错误:不是抽象的,并且不会覆盖Observer中的抽象方法onComplete()
错误:方法没有覆盖或实现超类型的方法
错误:不兼容的类型:io.reactivex.Scheduler无法转换为rx.Scheduler
这是gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.shiz.fetchimagerxretro"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.android.support:recyclerview-v7:27.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:cardview-v7:27.1.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.17'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.4.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}