我制作了一种从画廊中挑选图像的方法。
首先,我问用户是否要为其画廊授予许可,他是否确认以及何时选择照片,我想使用retrofit2在我的服务器上发送该照片的路径。
onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && requestCode == IMAGE_PICK_CODE && data!=null){
contactImage.setImageURI(data.getData());
photoPath = data.getData().getPath();
//photoPath is a type of string
}
}
将Photo对象添加到服务器的方法:
if(photoPath.equals("")){
Toast.makeText(this, "Please select photo!", Toast.LENGTH_SHORT).show();
return true;
}
Photo p = new Photo();
p.setId(hashCode());
p.setPath(photoPath);
//when I send some random string instead of photoPath it works and response code is 200
//but with this it goes into onResponse() method but response code is 400
p.setContact(newContact.getId());
Retrofit retrofit1 = new Retrofit.Builder()
.baseUrl(Url.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RestService restService1 = retrofit1.create(RestService.class);
Call<Photo> call1 = restService1.createPhoto(p.getId(),p.getPath(),p.getContact());
call1.enqueue(new Callback<Photo>() {
@Override
public void onResponse(Call<Photo> call, Response<Photo> response) {
if (!response.isSuccessful()){
Log.i("STATUS CODE PHOTO", Integer.toString(response.code()));
return;
}
}
@Override
public void onFailure(Call<Photo> call, Throwable t) {
Log.i("STACK","TRACE photo");
t.printStackTrace();
return;
}
});
Intent in = new Intent(this,ContactsActivity.class);
startActivity(in);
return true;
我在哪里错了?