2016-08-15 25 views
0

我送使用retrofit2在Android 對象,我想該對象的數據在服務器保存在一個JSON文件發送檢索在servlet的服務器端對象。我試着檢索對象的數據在服務器空,但跟隨由該語句返回:如何在通過改造使用@Body註釋

req.getParameter("job_title") 

所以,我想知道如何在服務器端檢索對象的實際值?

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"); 
}  
} 

我使用以下卡類,其目的是要被髮送到服務器

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; 
} 
} 

這是包含服務接口服務方法,該方法在發送對象:

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); 
} 
+0

'GSON GSON =新GSON();卡片= gson.fromJson(req.getReader());' – EpicPandaForce

+0

@EpicPandaForce第二條語句給出錯誤。沒有合適的方法發現fromJson(BufferedReader) –

+0

哦,它是'gson.fromJson(req.getReader(),Card.class)' – EpicPandaForce

回答

0

創建JSON字符串從你的班級,並通過改造身體。 您可以使用any選項來執行此操作,之後您可以將其發送到服務器。

接口:

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

請求:

//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); 

響應:

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