Я хочу отправить запрос на сервер. И сделать новый пост в приложении.
Я отправляю запрос, но не вижу ответа. Я делаю это в моем почтальоне, и он работает правильно.
Это мой запрос к серверу от почтальона.
Я заполняю и отправляю поля.
Изображение почтальона 1
И он дает мне этот ответ.
post man image 2
Ну, теперь вы видите мой код
мой запрос
@POST("ads")
Call<PostCreate> getUpload(@Header("Authorization") String token, @Body PostCreate post);
моя модель класса
public PostCreate(String category, String title, String price, String description, String address, String tell) {
this.category = category;
this.title = title;
this.price = price;
this.description = description;
this.address = address;
this.tell = tell;
}
и моя деятельность createpost code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_post);
Log.d(TAG, "onCreate: started");
post_title = findViewById(R.id.post_title);
post_description = findViewById(R.id.post_description);
post_phone = findViewById(R.id.post_phone);
post_price = findViewById(R.id.post_price);
post_category = findViewById(R.id.post_category);
post_address = findViewById(R.id.post_address);
submit = findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = post_title.getText().toString().trim();
String description = post_description.getText().toString().trim();
String phone = post_phone.getText().toString().trim();
String price = post_price.getText().toString().trim();
String category = post_category.getText().toString().trim();
String address = post_address.getText().toString().trim();
Log.d(TAG, "onClick submit: called. ");
createPost(title, description, phone, price, category, address);
}
});
}
private void createPost(String title, String description, String phone, String price, String category, String address) {
PostCreate post = new PostCreate(title, description, price, phone, address, category);
SharedPreferences preferences = getSharedPreferences("Get_Token", MODE_PRIVATE);
final String token = preferences.getString("Token", null);
Call<PostCreate> call = RetrofitClient.getmInstance().getApi().getUpload("token " + token, post);
call.enqueue(new Callback<PostCreate>() {
@Override
public void onResponse(Call<PostCreate> call, Response<PostCreate> response) {
Log.d(TAG, "onResponse Retrofit: Called. ");
Log.i(TAG, "onResponse: submit" + response.body().toString());
Toast.makeText(CreatePost.this, "All set", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<PostCreate> call, Throwable t) {
Toast.makeText(CreatePost.this, "ERROR: " + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
И ошибка, которая показывает мне
ava.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.book.Activities.CreatePost$2.onResponse(CreatePost.java:73)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
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)
почему нулевой объект? Я заполняю все поля.
можете мне помочь?
Пожалуйста, не связывайте меня с другой страницей
я могу обнулить изображение.