xiaoduc 2015-09-15 12:27 采纳率: 50%
浏览 4021
已采纳

android post方式提交中文数据到服务器乱码问题

Android端代码




                AsyncHttpClient client = new AsyncHttpClient();
                //URLEncoder.encode(category, "utf-8");  
                RequestParams params = new RequestParams();

                if(path1!=null){
                    file1=new File(path1);
                }
                if(path2!=null){
                    file2=new File(path2);
                }
                params.put("shopname", proname);
                params.put("description", description);
                params.put("price", proprice);
                params.put("userphone",usernum);
                params.put("category", category);


                client.post(url, params, new AsyncHttpResponseHandler(){
                    @Override
                    public void onSuccess(String content) {
                        // TODO Auto-generated method stub
                        super.onSuccess(content);
                        if(content.equals("发布成功")){
                            Toast.makeText(homepage.this, content, 0).show();
                            name.setText("");
                            price.setText("");
                            number.setText("");
                            intro.setText("");
                        }else{Toast.makeText(homepage.this, "发布失败", 0).show();}

                    }
                });

服务器端代码:




                if(ServletFileUpload.isMultipartContent(request)){
                try{
                    ServletFileUpload upload = new ServletFileUpload(
                            new DiskFileItemFactory());

                    List<FileItem> items = upload.parseRequest(request); // 解析请求

                    int size = items == null ? 0 : items.size();
                    for (int i = 0; i < size; i++) {
                        FileItem item = (FileItem) items.get(i); // 获取方法消息体中的每一段内容。
                        if (item.isFormField()) {// 如果是普通表单项目
                            if (item.getFieldName().equals("shopname")) {
                                shop.setShopname(new String(item.getString().getBytes("ISO8859-1"), "UTF-8"));
                            }  else if (item.getFieldName().equals("description")) {
                                shop.setDescription(new String(item.getString().getBytes("ISO8859-1"), "UTF-8"));
                            } else if (item.getFieldName().equals("price")) {
                                shop.setPrice((df.format(Double.valueOf(item.getString()))));
                            } else if (item.getFieldName().equals("userphone")) {
                                shop.setUserphone(item.getString());
                            } else if (item.getFieldName().equals("category")) {
                                shop.setCategory(new String(item.getString().getBytes("ISO8859-1"), "UTF-8"));
                                a=new String(item.getString().getBytes("ISO8859-1"), "UTF-8");
                            }/*else if (item.getFieldName().equals("picture")) {
                                shop.setPicture(new String(item.getString().getBytes("ISO8859-1"), "UTF-8"));
                            }else if(item.getFieldName().equals("picture1")){
                                shop.setPicture1(new String(item.getString().getBytes("ISO8859-1"), "UTF-8"));
                            }*/else if(item.getFieldName().equals("userid")){
                                shop.setUserid(Integer.parseInt(new String(item.getString().getBytes())));
                            }

为啥我用jsp提交中文不乱码,Android提交乱码呢,我用了android-async-http-1.4.4.jar包,post方式提交数据到服务器的,求解决,大神快来吧,我才接触Android,谢谢啦

  • 写回答

7条回答 默认 最新

  • bdmh 移动开发领域优质创作者 2015-09-15 12:55
    关注

    双方统一编码合适就行了,比如utf8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置