2016-10-12 4 views
-1

Ich habe eine PHP-Datei, die alle Mitarbeiter aus einer Datenbank erhält, wenn ich es ausführen, um alle Mitarbeiter im JSON-Format zu erhalten.Wert Verbunden vom Typ java.lang.String kann nicht in JSONObject konvertiert werden

Dieser Code in Android Studio, die alle Mitarbeiter aus der Datenbank I

Wenn ich die app laufen bekommt die Daten nicht in Android Studio erhalten durch die PHP-Datei haben.

Es gibt diesen Fehler zurück:

Value Connected of type java.lang.String cannot be converted to JSONObject

im Bild

enter image description here

Dies ist der Java-Code in Android Studio:

package com.example.ibrahim.samplecrud; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.ArrayList; 
import java.util.HashMap; 

public class ViewAllEmployee extends AppCompatActivity implements ListView.OnItemClickListener { 

    private ListView listView; 

    private String JSON_STRING; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_view_all_employee); 
     listView = (ListView) findViewById(R.id.listView); 
     listView.setOnItemClickListener(this); 
     getJSON(); 
    } 


    private void showEmployee(){ 
     JSONObject jsonObject = null; 
     ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>(); 
     try { 
      jsonObject = new JSONObject(JSON_STRING); 
      JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY); 

      for(int i = 0; i<result.length(); i++){ 
       JSONObject jo = result.getJSONObject(i); 
       String id = jo.getString(Config.TAG_ID); 
       String name = jo.getString(Config.TAG_NAME); 

       HashMap<String,String> employees = new HashMap<>(); 
       employees.put(Config.TAG_ID,id); 
       employees.put(Config.TAG_NAME,name); 
       list.add(employees); 
      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     ListAdapter adapter = new SimpleAdapter(
       ViewAllEmployee.this, list, R.layout.list_item, 
       new String[]{Config.TAG_ID,Config.TAG_NAME}, 
       new int[]{R.id.id, R.id.name}); 

     listView.setAdapter(adapter); 
    } 

    private void getJSON(){ 
     class GetJSON extends AsyncTask<Void,Void,String>{ 

      ProgressDialog loading; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(ViewAllEmployee.this,"Fetching Data","Wait...",false,false); 
      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 
       JSON_STRING = s; 
       showEmployee(); 
      } 

      @Override 
      protected String doInBackground(Void... params) { 
       RequestHandler rh = new RequestHandler(); 
       String s = rh.sendGetRequest(Config.URL_GET_ALL); 
       return s; 
      } 
     } 
     GetJSON gj = new GetJSON(); 
     gj.execute(); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Intent intent = new Intent(this, ViewEmployee.class); 
     HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position); 
     String empId = map.get(Config.TAG_ID).toString(); 
     intent.putExtra(Config.EMP_ID,empId); 
     startActivity(intent); 
    } 
} 
+0

bearbeiten, was ist der Wert von 'JSON_STRING'? –

+0

geschützter void onPostExecute (String s) { super.onPostExecute (s); loading.dismiss(); JSON_STRING = s; showEmployee(); } –

+0

Zeigen Sie uns den Wert des 'JSON_STRING' - haben Sie es selbst überprüft? –

Antwort

0

Derzeit Ihre String, die erhalten vom Server ist

Connection sucessful{"result":[{"id":2,"name":A"},{"id","pop"},...]} 

und es ist kein gültiges JSON-Format

So sollten Sie den Server-Code für die Rückkehr richtigen JSON-Format wie

+0

das ist mein Servercode mein Freund^_^ –

+0

$ row [ 'id'], \t \t \t "name" => $ row [ 'name'] \t \t)); \t} \t \t // Anzeige des Array im JSON-Format \t Echo json_encode (array ('Ergebnis' => $ result)); \t \t mysqli_close ($ con); –

+0

einfach entfernen Sie die "Verbindung erfolgreich" aus der Antwort –

Verwandte Themen