gpt4 book ai didi

php - Android AsyncTask 无法从 php-mysql 中检索 JSON 字符串

转载 作者:行者123 更新时间:2023-11-30 22:15:36 25 4
gpt4 key购买 nike

我可以显示存储在“值”中的数组中的数据,但是当我尝试从数据库中获取数据时,我得到了

"E/Surface: getSlotFromBufferLocked: unknown buffer"

错误。我有 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);
}
}

最佳答案

修改上面的代码如下:

JSONObject jsonObj = new JSONObject(strJson);

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

关于php - Android AsyncTask 无法从 php-mysql 中检索 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38407947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com