gpt4 book ai didi

java - 上传图片时出现未知错误 :class anonymous class derived from asynchttpResponsehandler must either be declared abstract

转载 作者:行者123 更新时间:2023-12-01 17:43:12 24 4
gpt4 key购买 nike

我是新手如果可能的话请查看以下代码我不明白问题我的程序正在尝试将文件上传到服务器。但是在这部分代码中出现了一个我无法理解的错误

错误:从 asynchttpResponsehandler 派生的类匿名类必须声明为抽象或在 asynchttpResponsehandler 中实现抽象方法 onsuccess()

package com.ehhg.upap1;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.preference.PreferenceActivity;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.text.PrecomputedTextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.PrecomputedText;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import android.Manifest;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpRequest;
import com.loopj.android.http.*;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.FileSystemNotFoundException;
import java.security.acl.Permission;
import java.util.jar.Manifest.*;
import cz.msebera.android.httpclient.entity.mime.Header;


public void uploadImage(File file) {
AsyncHttpClient myClient = new AsyncHttpClient();

RequestParams params = new RequestParams();
try {
params.put("file", file);
} catch (FileNotFoundException e) {
}
myClient.post(url + "android/upload.php", params, new AsyncHttpResponseHandler() {

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
JSONObject jsonObject = new JSONObject(new String(responseBody));
if (jsonObject.getString("status").equals("success")) {
Picasso.with(getBaseContext())
.load(url + "android/uploads/" + jsonObject.getString("filename"))
.into(img);
}
} catch (JSONException e) {
e.printStackTrace();
}

}


public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getBaseContext(), new String(responseBody), Toast.LENGTH_LONG).show();
}
});
}

最佳答案

该错误表明 AsyncHttpResponseHandler 必须实现 onSuccess 函数。

编辑:检查您的导入后,如其他用户评论的那样,您导入了错误的 header 类。您需要导入 cz.msebera.android.httpclient.Header 而不是 cz.msebera.android.httpclient.entity.mime.Header 类。

查看下面的代码以进行理解

myClient.post(url + "android/upload.php", params, new AsyncHttpResponseHandler() {

// following line is uncommented
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, byte[] responseBody) {
try {
JSONObject jsonObject = new JSONObject(new String(responseBody));
if (jsonObject.getString("status").equals("success")) {
Picasso.with(getBaseContext())
.load(url + "android/uploads/" + jsonObject.getString("filename"))
.into(img);
}
} catch (JSONException e) {
e.printStackTrace();
}

}


public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getBaseContext(), new String(responseBody), Toast.LENGTH_LONG).show();
}
});

希望有帮助。

关于java - 上传图片时出现未知错误 :class anonymous class derived from asynchttpResponsehandler must either be declared abstract,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60911210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com