有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中从json获得空字符串

因此,当我使用postman(googleapp)向我的web服务器上的php文件发送post方法时,我得到了一个完整的回复,返回了所有正确的信息。但当我尝试在安卓上做同样的事情时,我每次都会得到[]的响应

@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) 个答案