java.lang.IllegalArgumentException:API 声明必须是接口

问题描述 投票:0回答:1

当我们单击需要在服务器中上传的按钮时,我正在编写代码以在开始时录制音频。我正在使用改造:我正在使用两种不同的改造 API。一个用于论坛数据,另一个用于上传音频。

我收到这个错误:

java.lang.IllegalArgumentException: API declarations must be interfaces.

活动:

private void uploadAudio() {

    Long date = new Date().getTime();

    Date current_time = new Date(Long.valueOf(date));
    String file_path = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getPath() + "/" + current_time + ".3gp";


    File audioFile = new File(file_path);
    RequestBody requestFile = RequestBody.create(MediaType.parse("audio/*"), audioFile);
    MultipartBody.Part audioPart = MultipartBody.Part.createFormData("audio", audioFile.getName(), requestFile);

    ApiService apiService = ApiClient.getClient().create(ApiService.class);
    Call<ResponseBody> call = apiService.uploadAudio(audioPart);
    call.enqueue(new retrofit2.Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
            if (response.isSuccessful()){
                if (response.body().toString().equals("200")){
                    Toast.makeText(getApplicationContext(), "Audio uploaded Successfully", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getApplicationContext(),"Audio Upload Failed", Toast.LENGTH_SHORT).show();
                }
            }

        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.toString(), Toast.LENGTH_SHORT).show();

        }
    });

API

public class ApiService {
    @Multipart
    @POST("retroapi2.php")
    Call<ResponseBody> uploadAudio(@Part MultipartBody.Part audio) {

        return null;
    }
}
public class ApiClient {
    private static final String BASE_URL = "http://mysite.in/atmedia/ud/";

    private static Retrofit retrofit;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
if($_SERVER['REQUEST_METHOD']=='POST'){

 //Getting post data 

 $fileName  =  $_FILES['audio']['name'];

$tempPath  =  $_FILES['audio']['tmp_name'];

$fileSize  =  $_FILES['audio']['size'];

$upload_path = 'upload/'; // set upload folder path 

require_once('retconfig.php');
  
 //Creating an SQL Query to insert into database 
 //Here you may need to change the retrofit_users because it is the table I created
 //if you have a different table write your table's name
 
 //This query is to check whether the username or email is already registered or not 
 $sql = "SELECT * FROM demo WHERE mobile='$mobile'";
 
 //If variable check has some value from mysqli fetch array 
 //That means username or email already exist 
 $check = mysqli_fetch_array(mysqli_query($con,$sql));
 
 //Checking check has some values or not 
 if(isset($check)){
 //If check has some value that means username already exist 
 echo 'username or email already exist';
 }else{ 
 //If username is not already exist 
 //Creating insert query 
      
if($fileSize < 5000000){
    move_uploaded_file($tempPath, $upload_path . $fileName); // move file from system temporary path to our upload folder path 
}
java php android retrofit2 native-android
1个回答
0
投票

这是你的问题:

public class ApiService {
    @Multipart
    @POST("retroapi2.php")
    Call<ResponseBody> uploadAudio(@Part MultipartBody.Part audio) {

        return null;
    }
}

ApiService
需要成为
interface

public interface ApiService {
    @Multipart
    @POST("retroapi2.php")
    Call<ResponseBody> uploadAudio(@Part MultipartBody.Part audio);
}

有关更多信息,请参阅Retrofit 文档

© www.soinside.com 2019 - 2024. All rights reserved.