Java 如何使用utf8将数据从Android发布到服务器

Java 如何使用utf8将数据从Android发布到服务器,java,android,retrofit2,Java,Android,Retrofit2,我想将数据从android发布到服务器,我的服务器语言是PHP。 发布数据时,此数据使用奇怪的字符保存,例如:。此数据为utf8个字符,此数据不正常!。对于请求,我使用2 我的发帖请求是: private void sendComment(String cmPostID, String name, String email, String content) { Gson gson = new GsonBuilder() .setLenient()

我想将数据从android发布到服务器,我的服务器语言是PHP。
发布数据时,此数据使用奇怪的字符保存,例如:
。此数据为utf8个字符,此数据不正常!。对于请求,我使用
2

我的发帖请求是:

private void sendComment(String cmPostID, String name, String email, String content) {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        OkHttpClient client = new OkHttpClient();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        Retrofit_ApiInterface requestInterface = retrofit.create(Retrofit_ApiInterface.class);

        comment = new Comment();
        comment.setComment_post_ID(cmPostID);
        comment.setComment_author(name);
        comment.setComment_author_email(email);
        comment.setComment_content(content);

        ServerRequest request = new ServerRequest();
        request.setOperation(Constants.COMMENT);
        request.setComment(comment);

        Call<ServerResponse> response = requestInterface.cmOperation(request);
        response.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                ServerResponse resp = response.body();
                if (resp.getResult().equals(Constants.SUCCESS)) {
                    sendLoad.setVisibility(View.INVISIBLE);
                    TastyToast.makeText(context, StringEscapeUtils.unescapeHtml4(resp.getMessage()), TastyToast.LENGTH_LONG,
                            TastyToast.SUCCESS);
                    closeCommentDialog();
                } else {
                    sendCommentImage.setVisibility(View.VISIBLE);
                    sendLoad.setVisibility(View.INVISIBLE);
                    TastyToast.makeText(context, StringEscapeUtils.unescapeHtml4(resp.getMessage()), TastyToast.LENGTH_LONG,
                            TastyToast.ERROR);
                }
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {
                sendLoad.setVisibility(View.INVISIBLE);
                sendCommentImage.setVisibility(View.VISIBLE);
                TastyToast.makeText(context, "Faild, send Again please", TastyToast.LENGTH_LONG, TastyToast.ERROR);
            }
        });
}
private void sendcoment(字符串cmPostID、字符串名称、字符串电子邮件、字符串内容){
Gson Gson=new GsonBuilder()
.setLenient()
.create();
OkHttpClient=新的OkHttpClient();
改装改装=新改装.Builder()
.baseUrl(常量.BASE\u URL)
.客户(客户)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
Reformation\u ApiInterface requestInterface=Reformation.create(Reformation\u ApiInterface.class);
注释=新注释();
comment.setComment\u post\u ID(cmPostID);
comment.setComment_作者(姓名);
comment.setComment\u author\u电子邮件(电子邮件);
comment.setComment_content(content);
ServerRequest=newserverrequest();
request.setOperation(Constants.COMMENT);
request.setComment(comment);
调用响应=requestInterface.cmOperation(请求);
response.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
ServerResponse resp=response.body();
if(resp.getResult().equals(Constants.SUCCESS)){
sendLoad.setVisibility(View.INVISIBLE);
TastyToast.makeText(上下文,StringEscapeUtils.unescapeHtml4(resp.getMessage()),TastyToast.LENGTH\u LONG,
成功);
closeCommentDialog();
}否则{
sendCommentImage.setVisibility(View.VISIBLE);
sendLoad.setVisibility(View.INVISIBLE);
TastyToast.makeText(上下文,StringEscapeUtils.unescapeHtml4(resp.getMessage()),TastyToast.LENGTH\u LONG,
TastyToast.ERROR);
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
sendLoad.setVisibility(View.INVISIBLE);
sendCommentImage.setVisibility(View.VISIBLE);
makeText(上下文,“失败,请再次发送”,TastyToast.LENGTH\u LONG,TastyToast.ERROR);
}
});
}

如何解决此问题,并使用UTF8发送数据?

您可以发布日志吗?如果请求头和有效负载可用,这将很有帮助。@SamuelKok,如何将头设置为请求?你能帮我吗,我的朋友?请将标题添加到请求中,如下所示。你发送的有效载荷是多少,我不知道是服务器读取错误还是你的android应用程序编码错误。@SamuelKok,我应该在标题中写什么?你能帮我吗?我不知道问题出在哪里,所以我不能就此提出建议。除非您已经发布了日志,否则改装请求的请求标头和有效负载。您可以发布日志吗?如果请求头和有效负载可用,这将很有帮助。@SamuelKok,如何将头设置为请求?你能帮我吗,我的朋友?请将标题添加到请求中,如下所示。你发送的有效载荷是多少,我不知道是服务器读取错误还是你的android应用程序编码错误。@SamuelKok,我应该在标题中写什么?你能帮我吗?我不知道问题出在哪里,所以我不能就此提出建议。除非您已经发布了改装请求的日志、请求标题和有效负载。