org.json.JSONException:值类型java.lang.String的异常无法转换为JSONArray

时间:2015-08-12 12:57:30

标签: java php android json

我得到了这个Excepion两天

  

“org.json.JSONException:值类型java.lang.String的异常无法转换为JSONArray”,

我在网上搜索但我无法解决它。

它在浏览器中给出了我想要的响应,但我不知道android有什么问题!

请帮帮我。 这是我的代码

private class SigninActivity extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... arg0) {
        String newString="";
        try {
            String tonTexte = loginDisplay.getText().toString();
            String tonTexte1 = passDisplay.getText().toString();
            String link = "http://192.168.56.1/projects/stage/atelier.php";
            String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(tonTexte, "UTF-8");
            data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(tonTexte1, "UTF-8");
            URL url = new URL(link);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.flush();           
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            // Read Server Response
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                break;
            }
            return sb.substring(sb.indexOf("{"),sb.lastIndexOf("}") + 1);
        } 
        catch (Exception e) {
            return new String("Exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String result) {
        TextView a = (TextView) findViewById(R.id.aff);
        JSONObject json_data=null;
        TextView b = (TextView) findViewById(R.id.aff1);
        //recuperation des donnees json
        try{
            //String jsonObjRecv = JSONGet.getJSONfromURL(URL_LIST);
            if(!TextUtils.isEmpty(result)) {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    Log.i("log_tag", "cin: " + json_data.getString("nom"));
                    donnees.add(json_data.getString("nom"));
                    donnees1.add(json_data.getString("atelier"));                  
                }
                for (String elem : donnees) {
                    a.setText(elem);
                }
                for (String elem : donnees1) {
                    b.setText(elem);
                }
            }
            else{
                Log.w("json", "result is null");
            }
        }
        catch(JSONException e){
            Log.i("tagjsonexp",""+e.toString());
        } 
        catch (ParseException e) {
            Log.i("tagjsonpars", "" + e.toString());
        }
    }
}

这里是php代码

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
$base = mysql_connect ('localhost', 'root', '');  
mysql_select_db ('stage', $base) ;  
$output=array();
$username = $_POST['username'];
$password = $_POST['password'];
$sql=mysql_query("SELECT CIN FROM authentification where login= '".$username."'   AND password = '".$password. "'");
//$sql=mysql_query("SELECT CIN FROM authentification where login= 'amani'   AND password = '123' ");
if (!$sql) {
    die(mysql_error());
}
while($data = mysql_fetch_array($sql))
{
$a=$data['CIN'];
}
    $sql1=mysql_query("SELECT nom,atelier FROM controlleur where CIN= '".$a."'");
while($row=mysql_fetch_assoc($sql1))
{
$output[]=$row;

//print(json_encode(array($output));
print(json_encode(array("NomTableau" => $output)));
mysql_close();
?>

1 个答案:

答案 0 :(得分:1)

假设您的PHP脚本生成了正确的JSON。尝试更改此部分

JSONArray jArray = new JSONArray(result);

到此。

JSONObject jObject = new JSONObject(result);
JSONArray jArray = jObject.getJSONArray("NomTableau");