Java Android:在所有异步调用完成时通知
我被困在一个实现中。我正在开发一个Android应用程序,在其中集成了第三方SDK(库)并调用其API。SDK的函数调用实际上是异步调用(由库调用的WebAPI调用),它给出响应(成功或失败) 现在,我试图用代码来解释这种情况Java Android:在所有异步调用完成时通知,java,android,asynchronous,observer-pattern,greenrobot-eventbus,Java,Android,Asynchronous,Observer Pattern,Greenrobot Eventbus,我被困在一个实现中。我正在开发一个Android应用程序,在其中集成了第三方SDK(库)并调用其API。SDK的函数调用实际上是异步调用(由库调用的WebAPI调用),它给出响应(成功或失败) 现在,我试图用代码来解释这种情况 for (................) { AsyncAPICall (SuccessListener { onSuccess() { for (................) {
for (................) {
AsyncAPICall (SuccessListener {
onSuccess() {
for (................) {
AsyncAPICall (SuccessListener {
onSuccess() {
for (................) {
AsyncAPICall (SuccessListener, ErrorListener);
}
}
}, ErrorListener);
}
}
}, ErrorListener);
}
我想要通知或回调,或者被所有异步调用都已完成的消息通知。异步调用在不同的线程中运行,由于for循环,许多调用将同时执行
是否有任何机制在所有异步调用收到响应后通知我,或者我需要手动执行某些操作
有什么帮助吗?@Khushbu Shah,我更新了我的答案,有点变化:)(很长)。
为了确保它正常工作,我创建了一个独立的工作示例,并使用此API进行测试:
解决方案2:在顺序调用多个任务时使用,以前任务的所有结果都是最终任务的输入(例如:上传头像图像和封面图像后,调用api使用这些图像URL创建新用户): AndroidManifest app/build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
// Exclude the version that the android plugin depends on.
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "app.com.rxretrofit"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
provided 'org.projectlombok:lombok:1.16.6'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'io.reactivex:rxandroid:1.2.1'
}
型号
package app.com.rxretrofit;
import com.google.gson.annotations.SerializedName;
/**
* -> Created by Think-Twice-Code-Once on 11/26/2017.
*/
public class Posts {
@SerializedName("userId")
private int userId;
@SerializedName("id")
private int id;
@SerializedName("title")
private String title;
@SerializedName("body")
private String body;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
restposervice.java
package app.com.rxretrofit;
import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
/**
* -> Created by Think-Twice-Code-Once on 11/26/2017.
*/
public interface RestPostsService {
@GET("{id}")
Observable<Posts> getPostsById(@Path("id") int id);
}
package app.com.rx改装;
导入文件2.http.GET;
导入2.http.Path;
进口接收。可观察;
/**
*->由三思而后行代码于2017年11月26日创建。
*/
公共接口RESTPostService{
@获取(“{id}”)
可观测的getPostsById(@Path(“id”)int-id);
}
顺便说一句,使用Rx+改型+匕首+MVP模式是一个很好的组合。我同意rxjava在这里会很好,但您的示例几乎没有意义,预期的zip()observeOn()和subscribeOn()至少会感谢“@思考两次代码”。首先,我了解了RxJava,然后,我会尝试这个解决方案。@TimCastelijns在给出这个答案之前,我看了Observable.zip(),但我认为这个解决方案更简单。如果你认为这是一个好的解决方案,你不知道RxJava是如何工作的,也许你是对的,我会尝试在这个案例中更深入,我希望你能给出更好的答案。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
// Exclude the version that the android plugin depends on.
configurations.classpath.exclude group: 'com.android.tools.external.lombok'
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "app.com.rxretrofit"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
provided 'org.projectlombok:lombok:1.16.6'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'io.reactivex:rxandroid:1.2.1'
}
package app.com.rxretrofit;
import com.google.gson.annotations.SerializedName;
/**
* -> Created by Think-Twice-Code-Once on 11/26/2017.
*/
public class Posts {
@SerializedName("userId")
private int userId;
@SerializedName("id")
private int id;
@SerializedName("title")
private String title;
@SerializedName("body")
private String body;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
package app.com.rxretrofit;
import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
/**
* -> Created by Think-Twice-Code-Once on 11/26/2017.
*/
public interface RestPostsService {
@GET("{id}")
Observable<Posts> getPostsById(@Path("id") int id);
}