无法在onSuccess和onFailure上@Override

时间:2019-09-05 21:46:46

标签: java android asynchronous

我根据发现的一些示例编写了以下代码。

代码如下:

private void fetchBooks(String query){
    progress.setVisibility(ProgressBar.VISIBLE);
    client = new BookClient();
    client.getBooks(query, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            try {
                // hide progress bar
                progress.setVisibility(ProgressBar.GONE);
                JSONArray docs = null;
                if(response != null) {
                    // Get the docs json array
                    docs = response.getJSONArray("docs");
                    // Parse json array into array of model objects
                    final ArrayList<Book> books = Book.fromJson(docs);
                    // Remove all books from the adapter
                    bookAdapter.clear();
                    // Load model objects into the adapter
                    for (Book book : books) {
                        bookAdapter.add(book); // add book through the adapter
                    }
                    bookAdapter.notifyDataSetChanged();
                }
            } catch (JSONException e) {
                // Invalid JSON format, show appropriate error.
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            progress.setVisibility(ProgressBar.GONE);
        }
    });

}

但是,在onFailureonSuccess处,我都有一个错误提示:

method does not override method from its superclass.

我读到它可能是由于错误的标题引起的,但是我使用了以下软件包,但我认为它应该可以工作:

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.loopj.android.http.JsonHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import cz.msebera.android.httpclient.Header;

我使用的依赖项是:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    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'
    implementation 'com.codepath.libraries:asynchttpclient:0.0.8'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'cz.msebera.android:httpclient:4.3.6'
    implementation 'com.loopj.android:android-async-http:1.4.6'
}

getBook的定义如下:

public void getBooks(final String query, JsonHttpResponseHandler handler) {
    try {
        String url = getApiUrl("search.json?q=");
        client.get(url + URLEncoder.encode(query, "utf-8"), handler);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

我还读到我可能会使用Apache。 这些错误有什么原因吗?

谢谢

0 个答案:

没有答案