无法解析json数组

时间:2014-10-11 09:59:36

标签: java android json

我有这段代码连接到我的数据库。它位于免费的主机域中,当我尝试获取信息时,它会向我显示JsonException,告诉我该值无法转换为JsonArray错误

    public void getData(){
        txtView.setText("zoo62");
        String result = "";
        InputStream isr = null;


        System.out.println("zoo6");
        try{

            HttpClient client = new DefaultHttpClient();

            HttpPost post = new HttpPost("http://31.170.160.79/public_html/script2.php");
            HttpResponse response = client.execute(post);

            HttpEntity entity = response.getEntity();

            isr = entity.getContent();


        }catch(Exception e){
            txtView.setText( "error 1 "+ e.toString());
        }



        try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "utf-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line= reader.readLine()) != null){

                sb.append(line +"\n");
            }
            isr.close();
            result = sb.toString();

        }catch(Exception e){

            txtView.setText( "error 2 "+ e.toString());
        }

        int i = 0;
        try{

            txtView1.setText(result);
            String s =" " ;
            JSONArray array = new JSONArray(result);

            for( i=0;i<array.length();i++){

                JSONObject object = array.getJSONObject(i);

                s= s+   "name: " + object.getString("name")+"\n" +
                        "phone: "+ object.getString("phone") +"\n";
            }

            txtView.setText(s);
        }catch(Exception e){

            txtView.setText( "error 3 "+i+" "+ e.toString());
        }

        //txtView.setText("zoo63");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

我也想知道如何确定与服务器建立连接。

1 个答案:

答案 0 :(得分:0)

问题出在PHP的回复中。你必须得到像这样的JSONArray

 [{"name":"adra234","phone":"17"},{"name":"pavols","phone":"49"}]

为您提供此响应的test.php示例

<?php

//connect database   

$response = array();
$query = "SELECT phone, name FROM table_contacts ";
$result = mysql_query($query);
while($data = mysql_fetch_array($result))
{
$row = array();
$row["name"] = $data["name"];
$row["phone"] = $data["phone"];
array_push($response, $row);
}
echo json_encode($response);

?>

活动

public class MainActivity extends Activity {

TextView txtView;
TextView txtView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    txtView = (TextView) findViewById(R.id.txtView);
    txtView1 = (TextView) findViewById(R.id.txtView1);
    getData();
}


public void getData(){
    txtView.setText("zoo62");
    String result = "";
    InputStream isr = null;

    System.out.println("zoo6");
    try{

        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost("http://xxxxx/test.php");
        HttpResponse response = client.execute(post);

        Log.d("response from server ", response.toString());

        HttpEntity entity = response.getEntity();

        isr = entity.getContent();

        Log.d("isr ", isr.toString());


    }catch(Exception e){
        txtView.setText( "error 1 "+ e.toString());
    }

    try{

        BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line= reader.readLine()) != null){

            sb.append(line +"\n");
        }
        isr.close();
        result = sb.toString();

        Log.d("result ", sb.toString());

    }catch(Exception e){

        txtView.setText( "error 2 "+ e.toString());
    }


    int i = 0;
    try{

        txtView1.setText(result);
        String s =" " ;
        JSONArray array = new JSONArray(result);

        for( i=0;i<array.length();i++){

            JSONObject object = array.getJSONObject(i);

            s= s+   "name: " + object.getString("name")+"\n" +
                    "phone: "+ object.getString("phone") +"\n";
        }

        txtView1.setText(s);
    }catch(Exception e){

        txtView.setText( "error 3 "+i+" "+ e.toString());
    }


}



}