1

I can show data from an array stored in 'values', but when I try to get data from database, I am getting

"E/Surface: getSlotFromBufferLocked: unknown buffer"

error. I have php script and mysql database which has a 'question' column. I am not sure if I have problem in connection establishment or json parse or anywhere else.

My 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);
   }
}
Terry
  • 989
  • 8
  • 29
Raihan Ruhin
  • 31
  • 1
  • 6

1 Answers1

0

Edit above code as below:

JSONObject jsonObj = new JSONObject(strJson);

// Getting JSON Array node
JSONArray jArray = jsonObj.getJSONArray(YOUR_JSON_ARRAY_NAME);
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
  • In doinbackground replace JSONArray jArray = new JSONArray(strJson); with above lines. – Dhruvi Jul 16 '16 at 05:28
  • Should I replce YOUR_JSON_ARRAY_NAME with strJson? – Raihan Ruhin Jul 16 '16 at 05:32
  • Can you print json response? – Dhruvi Jul 16 '16 at 05:34
  • yes, writing Log.d(VersionsFragment.class.getSimpleName(), "JSON string" + strJson); above JSONObject jsonObj = new JSONObject(strJson); gives the following line: JSON string[{"id":"1","name":"what is java","address":"programming language"},{"id":"2","name":"what is oop","address":"object oriented programming"},{"id":"3","name":"why java used?","address":"mostly for android"}] – Raihan Ruhin Jul 16 '16 at 06:16
  • I have change php script, now its retrieve this string. JSON string[{"id":"1","question":"what is java","answer":"programming language"},{"id":"2","question":"what is oop","answer":"object oriented programming"},{"id":"3","question":"why java used?","answer":"mostly for android"}] – Raihan Ruhin Jul 16 '16 at 06:22
  • Sorry but then your previous code was correct please eliminate my code and put yours JSONArray jArray = new JSONArray(strJson); Because this link helped me understand that jsonarray without names need not create jsonobject.http://stackoverflow.com/questions/10164741/get-jsonarray-without-array-name – Dhruvi Jul 16 '16 at 06:28
  • OMG! my code works now. didn't change anything. http://9gag.com/gag/anYZ9Eo/my-code-works-but-i-don-t-know-why Thank you – Raihan Ruhin Jul 16 '16 at 06:33