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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
    • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序