简体   繁体   中英

how to retrieve object at server end in servlet when sent through retrofit using @Body annotation

I am sending Card object using retrofit2 from android and i want to save the data of that object at server in a json file. I tried to retrieve the object's data as follows but at server null is returned by this statement:

req.getParameter("job_title")

So,i want to know how to retrieve the actual values of the object at server end?

package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@WebServlet(name = "save_card" , urlPatterns = "/savecard")
public class SaveCard extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    System.out.println(req.getParameter("job_title"));
    resp.getWriter().print("true");
}    
}  

i am using following Card class whose object is to be sent to server

package com.go.gocard.model;


import com.google.gson.annotations.SerializedName;



public class Card {

private static final String FIELD_TITLE = "job_title";
private static final String FIELD_MOBILE = "phone";
private static final String FIELD_NAME = "name";
private static final String FIELD_EMAIL = "email";

@SerializedName(FIELD_TITLE)
private String title;
@SerializedName(FIELD_MOBILE)
private String mobile;
@SerializedName(FIELD_NAME)
private String name;
@SerializedName(FIELD_EMAIL)
private String email;

public Card(){

}

public Card(String name, String title, String email,  String mobile) {
    this.title = title;
    this.mobile = mobile;
    this.name = name;
    this.email = email;
}

public void setTitle(String title) {
    title = title;
}

public String getTitle() {
    return title;
}

public void setMobile(String mobile) {
    mobile = mobile;
}

public String getMobile() {
    return mobile;
}

public void setName(String name) {
    name = name;
}

public String getName() {
    return name;
}

public void setEmail(String email) {
    email = email;
}

public String getEmail() {
    return email;
}


@Override
public String toString(){
    return "title = " + title + ", mobile = " + mobile + ", name = " + name + ", email = " + email;
}
}

This is the service interface containing service method which is sending object:

package com.go.gocard.card;

import com.go.gocard.model.Card;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;


public interface CardService {

@POST("savecard")
Call<String> sendCard(@Body Card card);
}

Create JSON string from your class and send it via retrofit as Body. You can use any options to do this and after that, you can send it to the server.

Interface:

@POST("savecard")
Call<JsonElement> sendCard(@Body RequestBody card);
}

Request:

  //resultJson - json in String format, which you create from your class
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), resultJson)
    Call<JsonElement> result = service.sendCard(requestBody);

Response:

public void onResponse(Call<JsonElement> call, retrofit2.Response<JsonElement> response) {
            JsonElement jsonElement = response.body(); 
            String responseInJson = jsonElement.toString;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM