求教eclipse客户端传送数据乱码问题

发布于 2021-11-27 20:01:24 字数 15463 浏览 845 评论 2

case 1:

            user_name = name.getText().toString();

            password = pass.getText().toString();

            type = "student";

            new ConnationThread().start();

            break;

————————————————

private class ConnationThread extends Thread {

        private Sevlect sevlect;

        private String name,password,age,sex,tel,_id,class_id;

        @Override

        public void run() {

            // TODO Auto-generated method stub

            super.run();

            sevlect = new Sevlect();

            ArrayList<NameValuePair> params1 = new ArrayList<NameValuePair>();

            params1.add(new BasicNameValuePair("user_name", MainActivity.this.user_name));

            params1.add(new BasicNameValuePair("password", MainActivity.this.password));

            params1.add(new BasicNameValuePair("type",MainActivity.this.type));

            HttpResponse mHttpResponse1 = sevlect.doPost(url, params1);

            //判断是否成功

            if(mHttpResponse1.getStatusLine().getStatusCode() == 200) {

                String mass = null ;

                try {

                    //获取数据

                    InputStream is = mHttpResponse1.getEntity().getContent();

                    byte[] tmp = new byte[1024];

                    int n = -1;

                    //                    myHandler.sendEmptyMessage(mHttpResponse1.getStatusLine().getStatusCode());

                    while((n = is.read(tmp)) != -1) {

                        Log.i("FSLog", "get rsp content登录 = " + new String(tmp, 0, n));

                        mass = new String(tmp,0,n);

                    }

                    JSONObject jsonObject = new JSONObject(mass);

                    JSONArray jsonArray = null;

                    Message msg = handle.obtainMessage();

                    if (type == "teacher") {

                        msg.what = TEACHER;

                        jsonArray = jsonObject.getJSONArray(type);

                    }else if (type == "student") {

                        msg.what = STU;

                        jsonArray = jsonObject.getJSONArray(type);

                    }else if (type == "admin") {

                        msg.what = ADMIN;

                        jsonArray = jsonObject.getJSONArray(type);

                    }

                    list = new ArrayList<String>();

                    for (int i = 0; i < jsonArray.length(); i++) {

                        JSONObject JS = (JSONObject) jsonArray.get(i);

                        if (type == "teacher") {

                            name = JS.getString("user_name");

                            password = JS.getString("password");

                            age = JS.getString("age");

                            sex = JS.getString("sex");

                            tel = JS.getString("tel");

                            _id = JS.getString("_id");

                            class_id = JS.getString("class_id");

                        }else if (type == "student") {

                            name = JS.getString("user_name");

                            password = JS.getString("password");

                            age = JS.getString("age");

                            sex = JS.getString("sex");

                            tel = JS.getString("tel");

                            _id = JS.getString("_id");

                        }else if (type == "admin") {

                            name = JS.getString("user_name");

                            password = JS.getString("password");

                            _id = JS.getString("_id");

                        }

                        list.add(name);

                        list.add(age);

                        list.add(sex);

                        list.add(tel);

                        list.add(_id);

                        list.add(password);

                        list.add(class_id);

                    }

                    msg.obj = list;

                    handle.sendMessage(msg);

                } catch (Exception e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                    Log.e("FSLog", e.toString());

                }

            }else if (mHttpResponse1.getStatusLine().getStatusCode() == 800) {

                Log.e("FSLog","jkjkljkl登录");

                try {

                    //获取数据

                    InputStream is = mHttpResponse1.getEntity().getContent();

                    byte[] tmp = new byte[1024];

                    int n = -1;

                    //                    myHandler.sendEmptyMessage(mHttpResponse1.getStatusLine().getStatusCode());

                    while((n = is.read(tmp)) != -1) {

                        Log.i("FSLog", "get rsp content = " + new String(tmp, 0, n));

                    }

                }

                catch (Exception e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                    Log.e("FSLog", e.toString());

                }

            }

        }

    }

myeclipse服务端获取到的名字就是乱码(user_name = name.getText().toString();获取到的是中文)。百思不得其解。user_name=URLEncoder.encode(user_name, "utf-8");转码吧,不知道是位置不对还是,并木有什么作用,要么还是???乱码,要么就是%E5%B0%8F%E5%8D%8E。。。求指导。。。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2021-11-30 03:51:48

web服务器用的是post请求,现在主要问题就是客户端弄成utf8,不知道怎么办

等风来 2021-11-29 18:34:12

要看你之前在web页面使用get方式还是post的方式,还有客户端传过来的也要是utf-8格式的才行

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文