doudai3012 2015-11-17 18:15
浏览 133

从android中的json获取空字符串

So when i use postman(google app) to send a post method to my php file on my web server, i got a full respond back with all the correct information. but when i am trying to do the same thing on android, i am getting [] in response everytime.

@Override
    protected Profile doInBackground(Void... params) {
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("name", user.name));
        dataToSend.add(new BasicNameValuePair("password", user.password));
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpParams, CONNECTION_TIMEOUT);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpPost post = new HttpPost("http://www.secretvoice1.com/getData.php");
        Profile returnUser = null;
        try {

            HttpResponse httpResponse = client.execute(post);
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
            client.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            Log.d("11111:", httpResponse.toString());
            String result = EntityUtils.toString(entity);
            System.out.println(result);
            Log.d("222222", result);
            JSONObject jsonObject = new JSONObject(result);
            if (jsonObject.length() == 0) {
                returnUser = null;
                Log.d("this:", "here");
            } else {

                //String name = jsonObject.getJSONArray("name");
                //String password = jsonObject.getString("password");
                //String email = jsonObject.getString("email");
                returnUser = new Profile(user.name, user.password, user.email);
            }
            catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }
    }
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥20 数学建模,尽量用matlab回答,论文格式
    • ¥15 昨天挂载了一下u盘,然后拔了
    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能