samedi 9 mai 2015

Json in Android : Unexpected character (<) at position 0

I am trying to use Json for an android online db connection. Problem is no matter what i try i get the ever so popular Unexpected character (<) at position 0 error.

here is my method:

 public void getData(){
    String result = "";
    InputStream isr = null;

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http:link to my file.php");
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    }
    catch(Exception e){
        Log.e("log_tag", "Error in http connection " + e.toString());
        resulString="Couldn't connect to database";
    }
    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(newInputStreamReader(isr,"iso-8859-1"),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){
        Log.e("log_tag", "Error  converting result "+e.toString());
    }

    //parse json data
    try {
        String s = "";
        JSONParser parser_obj = new JSONParser();
        JSONArray jArray = (JSONArray) parser_obj.parse(result.toString());

        for(int i=0; i<jArray.length();i++){
            JSONObject json = jArray.getJSONObject(i);
            s = s +
                    "Id : "+json.getInt("Id")+" Name: "+json.getString("ShopName")+"\n"+
                    "Icon : "+json.getInt("ShopIcon")+"Description:"+json.getString("ShopDesc")+"\n\n";
        }

        resulString=s;

    } catch (Exception e) {
        // TODO: handle exception
        Log.e("log_tag", "Error Parsing Data "+e.toString());
    }

}

tried both get and post methods heres my php file:

!php1

the connection is established but theres no result and loggcat displays that error. Can you help me please?

Aucun commentaire:

Enregistrer un commentaire