简体   繁体   English

数组中的Android json响应

[英]Android json response in array

The following code shows only the first record. 以下代码仅显示第一条记录。 Can anyone correct my mistake. 谁能纠正我的错误。

Using this try i established connection with php 使用这个尝试,我建立了与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();
        }

Using this try block i printed all values 使用这个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"));
                }

Your question is not totally clear, but: 您的问题并不十分清楚,但是:

Probably the structure you want to output doesnt fit the given structure. 您要输出的结构可能与给定的结构不匹配。

Looking to this: http://developer.android.com/reference/org/json/JSONArray.html you can convert any JSON-Object to String with .toString()-method, to debug. 对此: http : //developer.android.com/reference/org/json/JSONArray.html可以使用.toString()方法将任何JSON对象转换为String进行调试。

Alternatively you can actively step through your code with debugging, to find the gap. 另外,您可以通过调试积极地逐步执行代码,以找出差距。

Refering to this: http://en.wikipedia.org/wiki/JSON it seems, that the structure you want to read, which is stored in "result" should look similar to this: 引用此: http : //en.wikipedia.org/wiki/JSON似乎要存储在“结果”中的结构应类似于以下内容:

[ { "id": "a", "name": "b" }, { "id": "a", "name": "b" } ] [{“ id”:“ a”,“ name”:“ b”},{“ id”:“ a”,“ name”:“ b”}]

Actually reading JSON can be pragmatically done like trial & error: debug step by step and check in Eclipse' "expressions"-tab for example, which command leads to which result :) 实际上可以像尝试和错误一样实际地读取JSON:逐步调试并检查Eclipse的“ expressions”选项卡,例如,哪个命令导致哪个结果:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM