如何使用java通过HTTP发送不同的语言文本
我正在尝试使用HTTP连接将json从一台服务器发送到另一台服务器 我有一个模型如何使用java通过HTTP发送不同的语言文本,java,http,http-post,Java,Http,Http Post,我正在尝试使用HTTP连接将json从一台服务器发送到另一台服务器 我有一个模型 Test{ private String transId; private String msisdn; private String msg; private String senderId; private String customerid; private String customerName; private String actiontypes;
Test{
private String transId;
private String msisdn;
private String msg;
private String senderId;
private String customerid;
private String customerName;
private String actiontypes;
}
在msg字段中,我将根据请求的语言以不同的语言模板发送消息
{"transId":null,"msisdn":null,"msg":"क्या हाल है","senderId":null,"customerid":null,"customerName":null,"actiontypes":null}
在上面的示例中,我正在发送印地语消息
这是我发布数据的代码
GrameenVerLangRequestModel obj = new GrameenVerLangRequestModel();
obj.setMsg("क्या हाल है");
String responseFromGrameen =
notification.postVernacularMessageToGrameen(gson.toJson(obj),logger);
这里是post方法
public String postVernacularMessageToGrameen(String body, Logger logger) {
logger.info("Start-postVernacularMessageToGrameen");
try {
StringBuffer responsebuf = new StringBuffer();
URL obj = new URL(url);
logger.info("Data :"+body);
System.out.println("Data :"+body);
HttpURLConnection con = (HttpURLConnection) obj .openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(body);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
if(responseCode == 200){
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
responsebuf.append(inputLine);
}
in.close();
logger.info("Response from Server : " + responsebuf);
return responsebuf.toString();
} else {
return responseCode+"";
}
}catch (Exception e) {
e.printStackTrace();
logger.info(e);
}finally {
logger.info("End-postVernacularMessageToGrameen");
}
return null;
}
这是我发布到另一个服务器之前的正文
Data :{"transId":null,"msisdn":null,"msg":"क्या हाल है","senderId":null,"customerid":null,"customerName":null,"actiontypes":null}
在发布到预期的服务器后,我将收到
Data received:{"transId":null,"msisdn":null,"msg":"M/> 9>2 9H","senderId":null,"customerid":null,"customerName":null,"actiontypes":null}
“msg”:“M/>9>2 9H”如何使其以预期语言可读
{"transId":null,"msisdn":null,"msg":"क्या हाल है","senderId":null,"customerid":null,"customerName":null,"actiontypes":null}
有人能帮我吗
谢谢
编辑:
我试图在接收服务器端解码,如下所示,但它给出了相同的响应
utf8EncodedString:{“transId”:null,“msisdn”:null,“msg”:“M/>9>2 9H”,“senderId”:null,“customerid”:null,“customerName”:null,“actiontypes”:null}
String rawString = receivedData;
ByteBuffer buffer = StandardCharsets.UTF_8.encode(rawString);
String utf8EncodedString =
StandardCharsets.UTF_8.decode(buffer).toString();
System.out.println("utf8EncodedString :"+utf8EncodedString);
使用与接收服务器上的请求服务器上使用的相同的字符编码。听起来像是编码问题,您使用的是什么web服务器。也许您需要将媒体类型和编码添加到producting属性中,比如@crimson589我已经用edit更新了我的问题,请检查。@Jocke我正在使用Restful web服务。