2016-10-12 16 views
-1

私はそれを実行するとデータベースからすべての従業員を取得するPHPファイルをJSON形式ですべての従業員に渡します。値の型はjava.lang.Stringの型をJSONObjectに変換できません

私は私は、Android Studioでデータを取得していないアプリを実行するとPHPファイル

により、データベースからすべての従業員を取得しますアンドロイドStudioでこのコード。

それは、このエラーが返されます。画像に

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

enter image description here

これは、Android StudioのJavaコードです:現在

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

のようなリターン正しいJSON形式のためのサーバーコードを編集する必要があり、有効なJSON形式

ではないでしょうか? –

+0

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

+0

私たちに 'JSON_STRING'の価値を教えてください - 自分でチェックしましたか? –

答えて

0

、受け取るあなたのStringサーバーからは

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

、それはあなたが `JSON_STRING`の値が何であるかを

+0

これは私のサーバーコードです。友人^ _^ –

+0

<?php \t //インポートデータベーススクリプト \t require_once( 'dbConnect。php '); \t \t //作成するSQLクエリ \t $ sqlを= "EMP SELECT * FROM"; \t \t //結果を得る \t $ r = mysqli_query($ con、$ sql); \t \t //空白の配列を作成する \t $ result = array(); \t \t //すべてのレコードをフェッチループ \tながら($行= mysqli_fetch_array($のR)){ \t \t \t \t //ブランク配列内の名前とIDを作成したプッシュ \t \t array_push($結果、アレイ( \t \t \t "ID" => $行[ 'ID']、 \t \t \t "名" => $行[ '名前'] \t \t))。 \t} \t \t // JSON形式でアレイを表示 \tエコーjson_encode(配列( '結果' => $結果))。 \t \t mysqli_close($ con); –

+0

あなたの応答から単に "Connection sucessful"を削除してください –

関連する問題