0

In my application I want POST some data, and this data get users and POST to server. For server requests I use Retrofit2.
For POST this data I should POST with json format, such as this :

{
  "email": "example@example.com",
  "username": "example",
  "password": "123",
}

After POST data I should check with this results for submit data has Ok ro Not.

{
  "status": 200,
  "Message": "",
  "data": true
}

I give Email, Username and Password with EditText from users, but how can I POST this data to server with Json format?

Please help me, I am amateur and I really need this help

  • This is probably what you're looking for: http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request – Valerio Santinelli May 04 '17 at 18:04
  • 2
    Possible duplicate of [How to POST raw whole JSON in the body of a Retrofit request?](http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request) – Valerio Santinelli May 04 '17 at 18:05

2 Answers2

2

Firstly, create a class for your request, for example, LoginRequest.java

public class LoginRequest {
private String email;
private String username;
private String password;

//getters and setters
}

Secondly, create a class for your response, LoginResponse.java

public class LoginResponse {
private Integer status;
private String Message;
private Boolean data;

//getters and setters
}

Finally, in your interface add this method:

public interface MiApiInterface {
    @POST("yourResourceName") Call<LoginResponse> login(@Body LoginRequest request);
}

I hope It could help you, just ask me if you have more question.

have you realised that the return of the login method is a Call, it is for a async call, you could use it like this on your activity:

firstly, create a retrofit instance

Retrofit retrofit = ....

Secondly, create your interface instance like this:

MiApiInterface apiInterface = retrofit.create(MiApiInterface.class);

Finally, you could access the login method:

    LoginRequest request = new LoginRequest();
    request.set();
    ....

    Call<LoginResponse> responseCall = apiInterface.login(request);

    responseCall.enqueue(new Callback<LoginResponse>() {
public void onResponse(...){
LoginResponse loginResponse = response.body();
}

public void onFailure(...){
}
    }

To Convert Objects to Json automatically, you should add a Converter Factory on your retrofit builder:

Gson gson = new GsonBuilder().create();

Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create(gson))
...

dont forget import the Gson library on your gradle.

jmarkstar
  • 1,335
  • 14
  • 21
  • Thanks, I create this classes. how can on POST this data in MainActivity? how can I post this data in onResponse of retrofit? –  May 04 '17 at 15:40
  • Please help me my friend. I really need this. I am amateur :( please –  May 04 '17 at 15:40
  • I added more steps :D – jmarkstar May 04 '17 at 15:50
  • thanks man, I know how can I use Retrofit and I create my classes. but my problem is how can I convert my data (email, username, password) to json and send this json file to server? how can I it? –  May 04 '17 at 15:52
  • Oh, import gson and create a Converter factory as I have added – jmarkstar May 04 '17 at 15:58
  • can you send me your skype account? please –  May 04 '17 at 16:00
0

Here is a tutorial on Retrofit 2: http://www.vogella.com/tutorials/Retrofit/article.html

Alternatively, you can use Volley, it is a library specificaly designed to make http requests on Android. https://developer.android.com/training/volley/index.html

Glen Pierce
  • 4,401
  • 5
  • 31
  • 50
  • I want use retrofit. can you help me? –  May 04 '17 at 15:23
  • Document is very general, can you send me code my above data? please my friends, I really need this. please –  May 04 '17 at 15:28