2016-07-16 31 views
1

我可以從存儲在「價值」的數組顯示數據,但是當我試圖從數據庫中的數據,我越來越的Android的AsyncTask不能在JSON字符串檢索從PHP MySQL的

「E /表面:getSlotFromBufferLocked:未知緩衝區「

錯誤。我有一個'問題'欄的PHP腳本和MySQL數據庫。我不確定在連接建立或json解析或其他任何地方有問題。

我VersionsFragment.java

public class VersionsFragment extends ListFragment { 
public VersionsFragment() { 
} 


@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    //String[] versionName = getResources().getStringArray(R.array.version_names); //this works fine 
    new MyTask().execute(); 
} 

class MyTask extends AsyncTask<String, Integer, String[]> { 

    @Override 
    protected String[] doInBackground(String... params) { 
     String[] versionName = new String[0]; 
     ArrayList<String> scripts = new ArrayList<String>(); 
     try { 
      URL url = new URL("http://192.168.1.105/android/phpcall.php"); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      String strJson = ""; 
      String line; 
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      while ((line = br.readLine()) != null) { 
       strJson += line; 
      } 
      //Log.d(VersionsFragment.class.getSimpleName(), line); 
      JSONArray jArray = new JSONArray(strJson); 

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

       JSONObject jObject = jArray.getJSONObject(i); 

       String name = jObject.getString("question"); 

       scripts.add(name); 
      } 
      versionName = scripts.toArray(new String[scripts.size()]); 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return versionName; 
    } 

    protected void onPostExecute(String[] result){ 
     //for(int i=0;i<result.length;i++) 
      //Log.d(VersionsFragment.class.getSimpleName(), result[i]); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, result); 
     setListAdapter(adapter); 
    } 
} 

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    OnVersionNameSelectionChangeListener listener = (OnVersionNameSelectionChangeListener) getActivity(); 
    listener.OnSelectionChanged(position); 
    } 
} 

回答

0

編輯上面如下代碼:

JSONObject jsonObj = new JSONObject(strJson); 

// Getting JSON Array node 
JSONArray jArray = jsonObj.getJSONArray(YOUR_JSON_ARRAY_NAME); 
+0

替代代碼的哪個部分? –

+0

在doinbackground中替換JSONArray jArray = new JSONArray(strJson);與上面的線。 – Drv

+0

我應該用strJson代替YOUR_JSON_ARRAY_NAME嗎? –

相關問題