Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组中的Android json响应_Java_Android - Fatal编程技术网

Java 数组中的Android json响应

Java 数组中的Android json响应,java,android,Java,Android,以下代码仅显示第一条记录。有人能纠正我的错误吗 通过这次尝试,我与php建立了连接 try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/androidphp/1/index.php"); httppost.setEntity(new UrlEncodedFormE

以下代码仅显示第一条记录。有人能纠正我的错误吗

通过这次尝试,我与php建立了连接

try{    
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/androidphp/1/index.php");
            httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
            if(response.getStatusLine().getStatusCode() != 200){
                Log.d("MyApp", "Server encountered an error");
            }

try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8"));
            sb= new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = null;

            while((line = reader.readLine()) != null){
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        }
使用这个try块,我打印了所有的值

try{
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                for(int i=0; i<jArray.length(); i++){
                    json_data = jArray.getJSONObject(i);
                    outputStream.append("Id="+json_data.getString("id"));
                    outputStream.append("Name="+json_data.getString("name"));
                }
试试看{
jArray=新的JSONArray(结果);
JSONObject json_data=null;

对于(int i=0;i而言,您的问题并不完全清楚,但:

可能您想要输出的结构不适合给定的结构

注意:您可以使用.toString()-方法将任何JSON对象转换为字符串,以进行调试

或者,您可以通过调试积极地逐步完成代码,以找到差距

参考这一点:看起来,存储在“result”中的要读取的结构应该类似于:

[ { “id”:“a”, “名称”:“b” }, { “id”:“a”, “名称”:“b” } ]


实际上,阅读JSON可以像尝试与错误一样实用:一步一步地调试并签入Eclipse的“表达式”——例如,哪个命令导致哪个结果:)

Id=1 Name=XXX,但我在数据库中有5条记录。jArray的长度是多少?Place System.out.println(“jArray大小:+jArray.length())在for循环之前检查其中的项数。当索引始终为1时,以下4个名称将被覆盖。jArray长度为5。如何检查长度,因为System.out.println在android中不起作用。“Id=1”。每个记录都有此数据吗?