jeudi 24 mai 2018

String cannot be converted to JSONArray .. how i can fix it?

I am new in android, I want to read the data mysql but I have this error, the backend part is in laravel !!

public function GetMetiers() {
    $metiers = Metier::all();
    return $metiers->toJson();
}

la partie android

public class MainActivity extends AppCompatActivity {
     String sData;
     TextView tv;
      String Data = "";
      String result;
       InputStream isr;
   Context con;
      @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv=(TextView)findViewById(R.id.test);

    new getData().execute("");

     }
     private class getData extends AsyncTask<String, Void, String> {
    String name;

    @Override
    protected String doInBackground(String... params) {
        result = "";
        isr = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new 
        HttpPost("http://192.168.1.233/projet/public/api/getmetiers"); 
         //YOUR PHP SCRIPT ADDRESS
            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());
        }

     //convert response to string
        try {
            BufferedReader reader = new BufferedReader(new 
     InputStreamReader(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());
        }

        JSONObject j = null;
        try {
            JSONArray jArray = new JSONArray(result);

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


                Data=Data+"\n"+  json.getString("libelle_metier");
           }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error Parsing Data " + e.toString());
        }
        return "Executed";
    }
     @Override
    protected void onPostExecute(String result) {
        tv.setText(""+Data);
    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
    }}   



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire