hi i want send file to server with retrofit2 and alertDialog but i have somthing error .
my Request by retrofit2
@Multipart
@POST("ads")
Call<ResponseBody> getUpload(@Header("Authorization") String token
, @Part("title") RequestBody title
, @Part("description") RequestBody description
, @Part("price") RequestBody price
, @Part("tell") RequestBody tell
, @Part("address") RequestBody address
, @Part("category") RequestBody category
, @Part MultipartBody.Part image);
i create one floatingAction Button for add item
FloatingActionButton floatingActionButton = findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showList();
}
});
i create onActivityResult For me when I choose to notify me by changing the name of the Button
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_FROM_GALLERY_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
saveUri = data.getData();
btn_select.setText("Image Selected !");
}
}
i create showlist for show me layout add new post and me fill information with alertDialog
private void showList() {
Log.d(TAG, "showList: started");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Home.this);
alertDialog.setTitle("Add new post");
alertDialog.setMessage("please fill all information");
LayoutInflater layoutInflater = this.getLayoutInflater();
View view = layoutInflater.inflate(R.layout.add_new_post, null);
tv_title = view.findViewById(R.id.tv_title);
tv_category = view.findViewById(R.id.tv_category);
tv_tell = view.findViewById(R.id.tv_tell);
tv_price = view.findViewById(R.id.tv_price);
tv_description = view.findViewById(R.id.tv_description);
tv_address = view.findViewById(R.id.tv_address);
btn_select = view.findViewById(R.id.btn_select);
btn_upload = view.findViewById(R.id.btn_upload);
btn_select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseImage();
}
});
btn_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadImage(saveUri);
}
});
alertDialog.setView(view);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
i create chooseImage for select image for read image in my gallery.
private void chooseImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_FROM_GALLERY_REQUEST);
}
In the end i create uploadImage for send file to server
private void uploadImage(Uri uri) {
Log.d(TAG, "uploadImage: started");
tv_title = findViewById(R.id.tv_title);
tv_address = findViewById(R.id.tv_address);
tv_description = findViewById(R.id.tv_description);
tv_price = findViewById(R.id.tv_price);
tv_tell = findViewById(R.id.tv_tell);
tv_category = findViewById(R.id.tv_category);
File originalFile = new File(uri.toString());
RequestBody titlePart = RequestBody.create(MultipartBody.FORM, tv_title.getText().toString());
RequestBody addressPart = RequestBody.create(MultipartBody.FORM, tv_address.getText().toString());
RequestBody descriptionPart = RequestBody.create(MultipartBody.FORM, tv_description.getText().toString());
RequestBody pricePart = RequestBody.create(MultipartBody.FORM, tv_price.getText().toString());
RequestBody tellPart = RequestBody.create(MultipartBody.FORM, tv_tell.getText().toString());
RequestBody categoryPart = RequestBody.create(MultipartBody.FORM, tv_category.getText().toString());
RequestBody imagePart = RequestBody.create(MediaType.parse(getContentResolver().getType(uri)),
FileUtils.getFile(originalFile));
MultipartBody.Part file = MultipartBody.Part.createFormData("image", originalFile.getName(), imagePart);
SharedPreferences preferences = getSharedPreferences("Get_Token", MODE_PRIVATE);
final String token = preferences.getString("Token", null);
Call<ResponseBody> call = RetrofitClient.getmInstance().getApi().getUpload(token, titlePart, descriptionPart, pricePart, tellPart, addressPart, categoryPart, file);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
and show me this error
2019-05-29 12:12:46.647 19215-19215/com.example.book E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.book, PID: 19215
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.book.Activities.Home.uploadImage(Home.java:160)
at com.example.book.Activities.Home.access$200(Home.java:53)
at com.example.book.Activities.Home$3.onClick(Home.java:123)
at android.view.View.performClick(View.java:6261)
at android.widget.TextView.performClick(TextView.java:11163)
at android.view.View$PerformClick.run(View.java:23748)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Now tell me where I got it wrong . thanks