1

I am developing an android application which has an access to a php script and parse the result returned by the code php. The code php connects to the base and return a result. The problem is that I get an error : Error parsing data org.json.JSONException: Value

Thanks for your help :) Java code :

 public class ville extends Activity {
    TextView txt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout rootLayout = new LinearLayout(getApplicationContext());  
        txt = new TextView(getApplicationContext());  
        rootLayout.addView(txt);  
        setContentView(rootLayout);  

        // Définir le texte et appeler la fonction de connexion.  
        txt.setText("Connexion..."); 
        // Appeler la méthode pour récupérer les données JSON
        txt.setText(getServerData(strURL)); 
    }

    // Mettre l'adresse du script PHP
    // Attention localhost ou 127.0.0.1 ne fonctionnent pas. Mettre l'adresse IP local.
    public static final String strURL = "http://192.168.1.2/www/nouveaudossier/ville.php";

    private String getServerData(String returnString) {
        InputStream is = null;
        String result = "";
        // Envoyer la requête au script PHP.
        // Script PHP : $sql=mysql_query("select * from tblVille where Nom_ville like '".$_REQUEST['ville']."%'");
        // $_REQUEST['ville'] sera remplacé par L dans notre exemple.
        // Ce qui veut dire que la requête enverra les villes commençant par la lettre L
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("Nom_ville","L"));

        // Envoie de la commande http
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(strURL);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        }catch(Exception e){
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convertion de la requête en string
        try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result=sb.toString();
        }catch(Exception e){
            Log.e("log_tag", "Error converting result " + e.toString());
        }
        // Parse les données JSON
        try{
            JSONArray jArray = new JSONArray(result);

            for(int i=0;i<jArray.length();i++){
                 JSONObject json_data = jArray.getJSONObject(i);

                                 // Affichage ID_ville et Nom_ville dans le LogCat

                                Log.i("log_tag","ID_ville: "+json_data.getInt("ID_ville")+

                                         ", Nom_ville: "+json_data.getString("Nom_ville")

                                 );

                                 // Résultats de la requête

                                 returnString += "\n\t" + jArray.getJSONObject(i);

                             }

                         }catch(JSONException e){

                             Log.e("log_tag", "Error parsing data " + e.toString());

                         }

                         return returnString;

                     }

                 }

php code :

<?php
  mysql_connect("localhost","root","password");
  mysql_select_db("bdVille");
  $sql=mysql_query("select * from tblVille where Nom_ville like '".$_REQUEST['ville']."%'");
  while($row=mysql_fetch_assoc($sql))
  $output[]=$row;
  print(json_encode($output));
  mysql_close();
?>
4
  • Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray Commented Mar 2, 2012 at 21:20
  • 2
    more info? what's the full stack trace, what's the data you're parsing, and what libraries are you using? Commented Mar 2, 2012 at 21:23
  • 1
    How are we supposed to determine what went wrong if you do not give us the code and your exact input? Commented Mar 2, 2012 at 21:26
  • You're receiving HTML probably instead of json. that's what it says there at least. So you gotta fix your php output first. Commented Mar 2, 2012 at 21:29

1 Answer 1

1

ok, probably you are missing the content type header in php script, add the following line to the php script.

header('Content-type: application/json');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.