【问题标题】:Why am I getting empty string on my android application?为什么我的 android 应用程序上出现空字符串?
【发布时间】:2016-08-30 06:10:36
【问题描述】:

这是我现在使用的代码:

 try {
            JSONObject root = new JSONObject(s);
            //JSONObject userdata = root.getJSONObject("userdata");
            JSONArray userdata = root.optJSONArray("userdata");


            for(int i=0; i < userdata.length(); i++){
                JSONObject jsonObject = userdata.getJSONObject(i);

                NAME = jsonObject.getString("name");
                EMAIL = jsonObject.getString("email");
                PASSWORD = jsonObject.getString("password");
                //data += "Node"+i+" : \n NAME= "+ NAME +" \n EMAIL= "+ EMAIL +" \n PASSWORD= "+ PASSWORD +" \n ";
            }
        }

【问题讨论】:

  • 你能发布你的 Json 响应吗?
  • 您的代码或 JSON 有问题。对不起,我不能在霍格沃茨以外使用魔法,所以你必须发布更多细节,比如s字符串的内容,以及究竟是什么
  • 感谢大家的回复。这是我的 JSON:-> {"userdata":[]}
  • class BackGround extends AsyncTask { @Override protected String doInBackground(String... params) { String name = params[0];字符串密码 = 参数 [1];字符串数据="";诠释时间;
  • 试试 { URL url = new URL("http://*****/laravel5.1/blog/public/showusers");字符串 urlParams = "name="+name+"&password="+password; HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); OutputStream os = httpURLConnection.getOutputStream(); os.write(urlParams.getBytes()); os.flush(); os.close(); InputStream = httpURLConnection.getInputStream(); while((tmp=is.read())!=-1){ 数据+= (char)tmp; } is.close(); httpURLConnection.disconnect();返回数据; }

标签: javascript java android json


【解决方案1】:

而不是JSONArray userdata = root.optJSONArray("userdata"); 使用方法getJSONArray 并确保你已经传递了正确的键值 经检查,未发现语法错误

【讨论】:

  • 问题已解决...这只是一个简单的 UI 错误!错误出现在 XML 代码中!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 1970-01-01
  • 2020-03-15
相关资源
最近更新 更多