Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- главная активность
- public class registr extends AppCompatActivity {
- private TextView textViewResult;
- private JsonPlaceHolderApi jsonPlaceHolderApi;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_registr);
- textViewResult=findViewById(R.id.text_view_result);
- Retrofit retrofit = new Retrofit.Builder()
- .baseUrl("http://37.21.54.126/")
- .addConverterFactory(GsonConverterFactory.create())
- .build();
- jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
- createPost();
- }
- private void createPost(){
- Post post=new Post("heh" ,"heh");
- Call<Post> call = jsonPlaceHolderApi.createPost(post);
- call.enqueue(new Callback<Post>() {
- @Override
- public void onResponse(Call<Post> call, Response<Post> response) {
- if (!response.isSuccessful()) {
- textViewResult.setText("Code:" + response.code());
- return;
- }
- Post postResponse = response.body();
- String content="";
- content+="Code:" + response.code() + "\n";
- content+="ID:" + postResponse.getId()+"\n";
- content+="User:"+ postResponse.getLogin()+"\n";
- content+="Password"+postResponse.getPassword()+"\n";
- content+="Text"+postResponse.getText()+"\n\n";
- textViewResult.setText(content);
- }
- @Override
- public void onFailure(Call<Post> call, Throwable t) {
- textViewResult.setText(t.getMessage());
- }
- });
- }
- }
- интерфейс жсона
- public interface JsonPlaceHolderApi {
- @POST("json")
- Call<Post> createPost(@Body Post post);
- }
- класс пост
- public class Post {
- private String login;
- private Integer id;
- private String password;
- @SerializedName("body")
- private String text;
- public String getLogin() {
- return login;
- }
- public Integer getId() {
- return id;
- }
- public String getPassword() {
- return password;
- }
- public String getText() {
- return text;
- }
- public Post(String login, String password) {
- this.login = login;
- this.password = password;
- }
- public void setLogin(String login) {
- this.login = login;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement