Я использую библиотеку Retrofit в Android studio для связи с веб-сервером в http-запросах. По одному из моих запросов я отправляю почтовый запрос на сервер. В этой строке:
Call<UploadRoomResponse> call = service.UploadRoom("Bearer " + token, new UploadRoomRequest(new Vector<String>() , angels, RoomName));
Это выброшено:
throw new IllegalStateException("Could not execute method for android:onClick", e);
Это мой интерфейс IService:
public interface IService
{
@POST("api/users/login")
Call<LoginResponse> Login(@Body LoginRequest loginRequest);
@POST("api/users/signup")
Call<SignupResponse> Signup(@Body SignupRequest signupRequest);
@GET("api/users/rooms")
Call<GetRoomsResponse> GetRooms(@Header("Authorization") String token);
@POST("api/rooms")
Call<UploadRoomResponse> UploadRoom(@Header("Authorization") String token, UploadRoomRequest uploadRoomRequest);
}
Это мой ответ и классы запросов:
public class UploadRoomResponse extends BaseResponse
{
private int roomId;
public UploadRoomResponse(String message, int roomId)
{
super(message);
this.roomId = roomId;
}
public int GetRoomId()
{
return roomId;
}
}
public class UploadRoomRequest
{
private List<String> files;
private List<Float> angles;
private String roomName;
public UploadRoomRequest(List<String> files, List<Float> angles, String roomName)
{
this.files = files;
this.angles = angles;
this.roomName = roomName;
}
public List<String> GetFiles()
{
return files;
}
public List<Float> GetAngles()
{
return angles;
}
public String GetRoomName()
{
return roomName;
}
}
Функция докладчика:
private void communicateUploadRoom(String token, String url)
{
Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
IService service = retrofit.create(IService.class);
List<Float> angels = new Vector<Float>();
//adding all the angles of the images to the request
for(int i = 0; i < 2; i++)
{
angels.add(90 - Images.get(i).GetPitch());
}
angels.add(Math.abs(Images.get(2).GetPitch()));
Call<UploadRoomResponse> call = service.UploadRoom("Bearer " + token, new UploadRoomRequest(new Vector<String>() , angels, RoomName));
call.enqueue(new Callback<UploadRoomResponse>() {
@Override
public void onResponse(Call<UploadRoomResponse> call, Response<UploadRoomResponse> response)
{
UploadRoomResponse uploadRoomResponse = response.body();
if(uploadRoomResponse != null && uploadRoomResponse.GetMessage() == null)
{
ClearSteps();
View.ToastUpload("Room Uploaded");
}
else
{
View.ToastUpload(uploadRoomResponse.GetMessage());
}
}
@Override
public void onFailure(Call<UploadRoomResponse> call, Throwable t) {
}
});
}
Трассировка стека:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: c.www.roomodel, PID: 1299
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6591)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25948)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6614)
at android.view.View.performClickInternal(View.java:6591)
at android.view.View.access$3100(View.java:786)
at android.view.View$PerformClick.run(View.java:25948)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6806)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #2)
for method IService.UploadRoom
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:729)
at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:348)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:202)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy0.UploadRoom(Unknown Source)
at c.www.roomodel.app.Activities.UploadRoom.UploadRoomPresenter.communicateUploadRoom(UploadRoomPresenter.java:170)
Код активности:
package c.www.roomodel.app.Activities.UploadRoom;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import c.www.roomodel.R;
import c.www.roomodel.app.Activities.Camera.CameraActivity;
import c.www.roomodel.app.Activities.Info.InfoActivity;
import c.www.roomodel.app.Activities.Options.OptionsActivity;
import c.www.roomodel.app.Models.GlobalUser;
import c.www.roomodel.app.Models.Image;
import c.www.roomodel.app.Models.UploadStep;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.LayoutDirection;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.List;
import java.util.Vector;
import static java.security.AccessController.getContext;
public class UploadRoomActivity extends AppCompatActivity implements UploadRoomView{
public LinearLayout mainLayout;
public UploadRoomPresenter Presenter;
public Button uploadButton;
public Button clearButton;
public EditText RoomName;
//list of V icons of steps
public List<ImageView> Vs;
private static final int MY_CAMERA_REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_room);
RoomName = (EditText)findViewById(R.id.RoomName);
addNameListener();
uploadButton = (Button)findViewById(R.id.upload_button);
clearButton = (Button)findViewById(R.id.clear_button);
Vs = new Vector<ImageView>();
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_CAMERA_REQUEST_CODE);
}
mainLayout = (LinearLayout)findViewById(R.id.main_layout);
Presenter = new UploadRoomPresenter(this);
ChangeRoomName(Presenter.GetRoomName());
Bundle b = getIntent().getExtras();
if(b != null)
{
Presenter.SetImage(b.getInt("id"), new Image(b.getByteArray("data"), b.getFloat("pitch")));
}
Presenter.CheckAvailableImages();
}
//This function creates a button dynamically of a step
//it gets parameters:
//right - is the step positioned right
//background - background image of button
//(This function doesn't define a button click event)
private Button CreateStepButton(Boolean right, int background)
{
Button button = new Button(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.info_camera_height), getResources().getDimensionPixelSize(R.dimen.info_camera_height));
button.setBackgroundResource(background);
params.gravity = Gravity.CENTER_VERTICAL;
//if linear layout is positioned right
if(right)
{
params.leftMargin = getResources().getDimensionPixelSize(R.dimen.step_button_margin);
}
else
{
params.rightMargin = getResources().getDimensionPixelSize(R.dimen.step_button_margin);
}
button.setLayoutParams(params);
return button;
}
//This function creates a linear layout dynamically of a step
//it gets parameters:
//right - is the step positioned right
private LinearLayout CreateLinearLayout(Boolean right)
{
LinearLayout linearLayout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.step_width), getResources().getDimensionPixelSize(R.dimen.step_height));
linearLayout.setBackgroundResource(R.drawable.step_layout);
if(right)
{
params.rightMargin = getResources().getDimensionPixelSize(R.dimen.step_margin);
params.gravity = Gravity.RIGHT;
linearLayout.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
}
else
{
params.leftMargin = getResources().getDimensionPixelSize(R.dimen.step_margin);
params.gravity = Gravity.LEFT;
linearLayout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
params.topMargin = getResources().getDimensionPixelSize(R.dimen.step_top_margin);
linearLayout.setLayoutParams(params);
return linearLayout;
}
private TextView CreateText(Boolean right, String text)
{
TextView textView = new TextView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(getResources().getColor(R.color.black));
textView.setTypeface(Typeface.create("sans-serif-black", Typeface.NORMAL));
textView.setText(text);
textView.setLayoutParams(params);
return textView;
}
private ImageView CreateVIcon(boolean right)
{
ImageView v = new ImageView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.info_camera_height), getResources().getDimensionPixelSize(R.dimen.info_camera_height));
v.setImageResource(R.drawable.v);
params.gravity = Gravity.CENTER_VERTICAL;
//if linear layout is positioned right
if(right)
{
params.leftMargin = getResources().getDimensionPixelSize(R.dimen.step_button_margin);
}
else
{
params.rightMargin = getResources().getDimensionPixelSize(R.dimen.step_button_margin);
}
v.setLayoutParams(params);
return v;
}
public void CreateStep(Boolean right, UploadStep step, int id)
{
LinearLayout linearLayout = CreateLinearLayout(right);
linearLayout.setId(id);
ImageView v = CreateVIcon(right);
v.setAlpha(0);
Vs.add(v);
Button cameraButton = CreateStepButton(right, R.drawable.camera_button);
Button infoButton = CreateStepButton(right, R.drawable.info_button);
TextView textView = CreateText(right, step.GetTitle());
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(UploadRoomActivity.this, CameraActivity.class);
Bundle b = new Bundle();
b.putInt("id", ((View)v.getParent()).getId());
takePictureIntent.putExtras(b);
startActivity(takePictureIntent);
finish();
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, ((View)v.getParent()).getId());
}
}
});
infoButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Presenter.ChangeRoomName(RoomName.getText().toString());
Intent intent = new Intent(UploadRoomActivity.this, InfoActivity.class);
Bundle b = new Bundle();
UploadStep step = Presenter.GetStep(((View)v.getParent()).getId());
b.putInt("image", step.GetImage());
b.putInt("info", step.GetInfo());
b.putString("title", step.GetTitle());
intent.putExtras(b);
startActivity(intent);
finish();
}
});
linearLayout.addView(v);
linearLayout.addView(cameraButton);
linearLayout.addView(infoButton);
linearLayout.addView(textView);
mainLayout.addView(linearLayout);
}
@Override
public void CheckStep(int id)
{
Vs.get(id).setAlpha(255);
}
public void changeToOptions(View view)
{
startActivity(new Intent(UploadRoomActivity.this, OptionsActivity.class));
finish();
}
@Override
public void enableButton(boolean enable)
{
uploadButton.setEnabled(enable);
if(enable)
{
uploadButton.setBackgroundColor(getResources().getColor(R.color.enabledButton));
}
else
{
uploadButton.setBackgroundColor(getResources().getColor(R.color.disabledButton));
}
}
@Override
public void InvisibleV()
{
for(int i = 0; i < Vs.size(); i++)
{
Vs.get(i).setAlpha(0);
}
}
//This function is an 'on click' function for the clear button
//clears all images and V's
//clears all images and V's
public void ClearClick(View v)
{
Presenter.ClearSteps();
}
//This function is an 'on click' function for the upload button
//Uploads the images
public void UploadClick(View v)
{
Presenter.TryUploadRoom(GlobalUser.User.GetToken(), getResources().getString(R.string.baseUrl));
}
@Override
public void ClearRoomName()
{
RoomName.setText("");
}
private void addNameListener()
{
RoomName.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s)
{
Presenter.ChangeRoomName(RoomName.getText().toString());
Presenter.canEnableUpload(RoomName.getText().toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
@Override
public void ChangeRoomName(String name)
{
RoomName.setText(name);
}
@Override
public void ToastUpload(String s)
{
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
Что может быть причина возникшего исключения?
Спасибо.