Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. главная активность
  2.  
  3. public class registr extends AppCompatActivity {
  4. private TextView textViewResult;
  5. private JsonPlaceHolderApi jsonPlaceHolderApi;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_registr);
  10. textViewResult=findViewById(R.id.text_view_result);
  11. Retrofit retrofit = new Retrofit.Builder()
  12. .baseUrl("http://37.21.54.126/")
  13. .addConverterFactory(GsonConverterFactory.create())
  14. .build();
  15.  
  16. jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
  17.  
  18. createPost();
  19. }
  20.  
  21.  
  22.  
  23. private void createPost(){
  24. Post post=new Post("heh" ,"heh");
  25. Call<Post> call = jsonPlaceHolderApi.createPost(post);
  26. call.enqueue(new Callback<Post>() {
  27. @Override
  28. public void onResponse(Call<Post> call, Response<Post> response) {
  29. if (!response.isSuccessful()) {
  30. textViewResult.setText("Code:" + response.code());
  31. return;
  32. }
  33.  
  34. Post postResponse = response.body();
  35.  
  36. String content="";
  37.  
  38. content+="Code:" + response.code() + "\n";
  39. content+="ID:" + postResponse.getId()+"\n";
  40. content+="User:"+ postResponse.getLogin()+"\n";
  41. content+="Password"+postResponse.getPassword()+"\n";
  42. content+="Text"+postResponse.getText()+"\n\n";
  43.  
  44. textViewResult.setText(content);
  45. }
  46.  
  47. @Override
  48. public void onFailure(Call<Post> call, Throwable t) {
  49. textViewResult.setText(t.getMessage());
  50. }
  51. });
  52.  
  53. }
  54. }
  55.  
  56.  
  57. интерфейс жсона
  58. public interface JsonPlaceHolderApi {
  59.  
  60. @POST("json")
  61. Call<Post> createPost(@Body Post post);
  62. }
  63.  
  64. класс пост
  65. public class Post {
  66. private String login;
  67. private Integer id;
  68. private String password;
  69. @SerializedName("body")
  70. private String text;
  71.  
  72. public String getLogin() {
  73. return login;
  74. }
  75.  
  76. public Integer getId() {
  77. return id;
  78. }
  79.  
  80. public String getPassword() {
  81. return password;
  82. }
  83.  
  84. public String getText() {
  85. return text;
  86. }
  87.  
  88. public Post(String login, String password) {
  89. this.login = login;
  90. this.password = password;
  91.  
  92. }
  93.  
  94. public void setLogin(String login) {
  95. this.login = login;
  96. }
  97.  
  98. public void setPassword(String password) {
  99. this.password = password;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement