Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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();
?>
share|improve this question
Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray – user1231728 Mar 2 '12 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? – jex Mar 2 '12 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? – elusive Mar 2 '12 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. – Sergey Benner Mar 2 '12 at 21:29
I added the code. Can someone help me ??? – user1231728 Mar 4 '12 at 9:04
add comment (requires an account with 50 reputation)

1 Answer

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');
share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.