我正在编写代码记录音频在开始时,当我们点击按钮,它需要在服务器上上传。我正在使用改造:我使用两种不同的改造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
}
这就是你的问题:
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文档了解更多信息。