从android中的json获取空字符串

所以当我使用postman(谷歌应用程序)将帖子方法发送到我的网络服务器上的php文件时,我 得到了所有正确信息的完整回复。 但是当我试图在android上做同样的事情时,我每次都会得到[]响应。</ p>

  @Override 
protected Profile doInBackground(Void ... params) {
ArrayList&lt; NameValuePair&gt; dataToSend = new ArrayList&lt;&gt;();
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");
配置文件returnUser = null;
尝试{

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(“2222 22“,result);
JSONObject jsonObject = new JSONObject(result);
if(jsonObject.length()== 0){
returnUser = null;
Log.d(”this:“,”这里 “);
} 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(例外e){
e.printStackTrace();
}

返回null;
}
}
</ code> </ pre>
</ div>

展开原文

原文

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;
        }
    }

dongtuo5262
dongtuo5262 我试图在postman@HanletEscaño中查看这个
接近 5 年之前 回复
duanpei4455
duanpei4455 你为什么要求用户名和密码?
接近 5 年之前 回复
donglan8870
donglan8870 您确定要发送有效的名称和密码吗?因为我在这里测试了foo/bar并返回了[]
接近 5 年之前 回复
douzhanshen0657
douzhanshen0657 发布API文档
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐