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

时间:2017-03-16 08:20:01

标签: java android retrofit2

我想要从android到服务器的POST数据,我的服务器语言是PHP POST数据时,此数据保存为奇怪的字符,例如:وب تیحی。这个数据是utf8字符,这个数据不行!对于请求,我使用Retrofit2

我的POST请求是:

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

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

0 个答案:

没有答案