niaonao
niaonao
采纳率71.4%
2016-06-25 10:02

android 获取服务器返回的json 乱码

已结题

由安卓端发送带数据的请求,
服务器接收 json及解析都是正确的,
在返回 android 端之前控制台输出的也是正确的
到 android 端就乱码了
(本程序在英文状态下能正常显示使用,程序没问题)

(1. 发送请求前)
乱码1图片说明

(2. )
(3. 数据在服务器)
乱码2图片说明

(4. 安卓端再获取)
乱码3图片说明

部分处理代码,未成功解决问题

 JSONArray userarr = new JSONArray(userjson);
                JSONObject userobj = userarr.getJSONObject(0);

                byte[] byte1 = userobj.getString("gender").getBytes();
                String str1 = new String(byte1,"utf-8");
                Log.i(tag,"CLS: str1:"+str1+"$$$");

                String str4 = new String(byte1,"gbk");
                Log.i(tag,"CLS: str4:"+str4+"$$$");

                String str5 = new String(byte1,"gb2312");
                Log.i(tag,"CLS: str5:"+str5+"$$$");

                String gender2 = userobj.getString("gender");
                String str2 = new String(gender2.getBytes("utf-8"),"gbk");
                Log.i(tag,"CLS: str2:"+str2+"$$$");


                String str3 = new String(gender2.getBytes("utf-8"),"utf-8");
                Log.i(tag,"CLS: str3:"+str3+"$$$");
/*********************/
                /*****************************/
                String gender = userobj.getString("gender");
                String _gender = new String(gender.getBytes("iso-8859-1"),"gbk");    //编码解决3

                byte[] bytes = gender.getBytes();
                String str = new String(bytes, "gb2312");                           //编码解决4


                Log.i(tag,"CLS: _gender:"+_gender+"$$$");
                Log.i(tag,"CLS: str:"+str+"$$$");

对应结果:
处理图片说明

  • 点赞
  • 收藏
  • 复制链接分享

4条回答

  • ziyejinwei1994 ziyejinwei1994 5年前

    开发工具的编码进行设置成UTF-8了吗

    点赞 评论 复制链接分享
  • niaonao niaonao 5年前

    OutputStreamWriter osw = new OutputStreamWriter(os);
    问题已解决
    OutputStreamWriter osw = new OutputStreamWriter(os,"utf-8");

    点赞 评论 复制链接分享
  • niaonao niaonao 5年前

    安卓端

     //(3)学生登录
        public Student check(Student student){
            username = student.getUsername();
            password = student.getPassword();
            /***************************************/
            Log.i(tag,"CLS:username|password "+username+"|"+password+"$$$");
            /***************************************/
                try{
                    /***************************************/
                    Log.i(tag,"CLS:创建连接...$$$");
                    /***************************************/
                url = new URL(urlStudent);
                HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                conn.setRequestProperty("Charset", "UTF-8");          //编码解决1
    //发送数据
    
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Charset","UTF-8");
                conn.connect();                                          //创建链接
                /*********************************************/
                Log.i(tag,"CLUser:连接成功!$$$");
                Log.i(tag,"CLUser:数据传送准备...$$$");
                /*********************************************/
    
                //传送数据至服务器
                OutputStream os = _conn.getOutputStream();                //建立输出流,写入数据至流发送到服务器
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
    
                /*********************************************/
                Log.i(tag,"CL:username"+username+"$$$");
                Log.i(tag,"CL:password"+password+"$$$");
                JSONArray jsonArray = new JSONArray();                   //封装数据为 json串
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("username",username);
                jsonObject.put("password",password);
                jsonArray.put(jsonObject);
                /*********************************************/
                Log.i(tag,"CL:json"+jsonArray.toString()+"$$$");
    
                bw.write(jsonArray.toString());                         //将 json串写入流
                bw.flush();
                /*********************************************/
                Log.i(tag,"CLUser:数据已传送$$$");
                /*********************************************/
            if(bw!=null)
                bw.close();
            if(os!=null)
                os.close();
            if(osw!=null)
                osw.close();
    
    
    //接收数据
                    /***************************************/
                    Log.i(tag,"CLS:接收数据...$$$");
                    /***************************************/
                InputStream is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is,"utf-8");    //编码解决2
                    /**
                     * 乱码问题,接收参数 json时。
                     */
    
                BufferedReader br = new BufferedReader(isr);
                String userjson = br.readLine();
    
                    /*********************/
                //测试乱码 stringbuffer
                    StringBuffer result = new StringBuffer("");
                    final String string = br.readLine();
                    if(string!=null){
                        result.append(string);
                        if(!result.equals("error")){
                            JSONArray ja = new JSONArray(string);
                            JSONObject jo = ja.getJSONObject(0);
                            String s1 = jo.getString("gender");
                            Log.i(tag,""+s1);
                        }
                    }
                    /*********************/
    
                    /*********************/
                Log.i(tag,"CLS:userjson "+userjson.toString()+"$$$");
                if(!userjson.equals("error")){
                    JSONArray userarr = new JSONArray(userjson);
                    JSONObject userobj = userarr.getJSONObject(0);
    
                    byte[] byte1 = userobj.getString("gender").getBytes();
                    String str1 = new String(byte1,"utf-8");
                    Log.i(tag,"CLS: str1:"+str1+"$$$");
    
                    String str4 = new String(byte1,"gbk");
                    Log.i(tag,"CLS: str4:"+str4+"$$$");
    
                    String str5 = new String(byte1,"gb2312");
                    Log.i(tag,"CLS: str5:"+str5+"$$$");
    
                    String gender2 = userobj.getString("gender");
                    String str2 = new String(gender2.getBytes("utf-8"),"gbk");
                    Log.i(tag,"CLS: str2:"+str2+"$$$");
    
    
                    String str3 = new String(gender2.getBytes("utf-8"),"utf-8");
                    Log.i(tag,"CLS: str3:"+str3+"$$$");
    /*********************/
                    /*****************************/
                    String gender = userobj.getString("gender");
                    String _gender = new String(gender.getBytes("iso-8859-1"),"gbk");    //编码解决3
    
                    byte[] bytes = gender.getBytes();
                    String str = new String(bytes, "gb2312");                           //编码解决4
    
    
                    Log.i(tag,"CLS: _gender:"+_gender+"$$$");
                    Log.i(tag,"CLS: str:"+str+"$$$");
    
                    Log.i(tag,"CLS: id:"+userobj.getInt("id")+"$$$");
                    Log.i(tag,"CLS: username:$$$");
                    Log.i(tag,"CLS: gender:"+userobj.getString("gender")+"$$$");
                    Log.i(tag,"CLS: age:"+userobj.getInt("age")+"$$$");
                    Log.i(tag,"CLS: grade:"+userobj.getInt("grade")+"$$$");
    
    
                    s = new Student();
    
                    s.setId(userobj.getInt("id"));
                    s.setUsername(username);
                    s.setPassword(password);
                    s.setGender(userobj.getString("gender"));
                    s.setAge(userobj.getInt("age"));
                    s.setGrade(userobj.getInt("grade"));
    
                    /*****************************/
                    Log.i(tag,"CLS: s:"+s.getId()+"$$$");
                    Log.i(tag,"CLS: s:"+s.getUsername()+"$$$");
                    Log.i(tag,"CLS: s:"+s.getGender()+"$$$");
                    Log.i(tag,"CLS: s:"+s.getAge()+"$$$");
                    Log.i(tag,"CLS: s:"+s.getGrade()+"$$$");
    
                }
    
                if(is!=null)
                    is.close();
                if(isr!=null)
                    isr.close();
                if(br!=null)
                    br.close();
            } catch (JSONException jsone){jsone.printStackTrace();
            } catch (IOException ioe){ioe.printStackTrace();}
    
            return s;
        }
    
    点赞 评论 复制链接分享
  • niaonao niaonao 5年前

    服务器端

     package pers.rfeng.sm.android;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import pers.rfeng.sm.dao.StudentDao;
    import pers.rfeng.sm.dao.StudentDaoImpl;
    import pers.rfeng.sm.model.Student;
    
    public class CheckLoginStudent extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
        private String username;
        private String password;
        private Student s;                                          //要返回的对象
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doPost(request, response);
        }
    
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");                  //设置编码
    //      response.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            /*******************************************/
            System.out.println("CLStudent:接收到请求,数据处理中..."+"$$$");
            /*******************************************/
    
            /**
             * 建立输入输出流
             * 接收 android端数据
             * 解析数据
             * 返回验证结果
             */
            InputStream is = request.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,"utf-8");
            BufferedReader br = new BufferedReader(isr);
            String userjson = br.readLine();                        //获取 json串
            /*******************************************/
            System.out.println("CLStudent:userjson "+userjson.toString()+"$$$");
            /*******************************************/
            try{
                JSONArray jsonArray = new JSONArray(userjson);          //解析 json串,获取数据
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                username = jsonObject.getString("username");
                password = jsonObject.getString("password");
                /*******************************************/
                System.out.println("CLStudent:username|password "+username+"|"+password+"$$$");
                /*******************************************/
                StudentDao sm = new StudentDaoImpl();
                s = sm.checkLogin(username, password);
                /*******************************************/
                System.out.println("CLStudent:验证结束!$$$");
                /*******************************************/
                OutputStream os = response.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);
    
                if(s!=null){
                    /*******************************************/
                    System.out.println("CLStudent:验证成功!$$$");
                    /*******************************************/
                    JSONArray userarr = new JSONArray();                 //封装数据返回
                    JSONObject userobj = new JSONObject();
                    userobj.put("id", s.getId());
                    userobj.put("username", s.getUsername());
                    userobj.put("password", s.getPassword());
                    userobj.put("grade", s.getGrade());
                    userobj.put("age", s.getAge());
                    userobj.put("gender", s.getGender());
                    userarr.put(userobj);
                    /*******************************************/
                    System.out.println("CLStudent:userarr "+userarr.toString()+"$$$");
                    /*******************************************/
                    bw.write(userarr.toString());
                    bw.flush();
                }else{
                    /*******************************************/
                    System.out.println("CLStudent:验证失败!$$$");
                    /*******************************************/
                    bw.write("error");
                    bw.flush();
                }
                if(bw!=null)
                    bw.close();
                if(os!=null)
                    os.close();
                if(osw!=null)
                    osw.close();
                if(is!=null)
                    is.close();
                if(isr!=null)
                    isr.close();
                if(br!=null)
                    br.close();
            } catch(JSONException jsone){jsone.printStackTrace();
            }
    
        }
    
    }
    
    
    点赞 评论 复制链接分享

相关推荐