我正在尝试将数组从我的 php 脚本解析到我的 android 应用程序。我的安卓代码
public void func4(View view)throws Exception
{final String TAG_CONTACTS = "response";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams rp = new RequestParams();
rp.put("pLat", "SELECT officer_name FROM iwmp_officer");
client.post("http://10.0.2.2/conc3.php", rp, new AsyncHttpResponseHandler() {
public final void onSuccess(String response) {
// handle your response here
ArrayList<String> User_List = new ArrayList<String>();
try
{
JSONArray jArray = new JSONArray(response.toString());
// JSONObject jsonObject = new JSONObject(response);
for (int i = 0; i < jArray.length(); i++)
{
JSONObject json_data = jArray.getJSONObject(i);
User_List.add(json_data.getString(TAG_CONTACTS));
String s = User_List.get(0).toString();
tx.setText(s);
}
}
catch (Exception e)
{
tx.setText((CharSequence) e);
}
}
@Override
public void onFailure(Throwable e, String response) {
// something went wrong
tx.setText(response);
}
});
}
现在我正在从我的 php 代码中发送或 ECHO 一个 JSON 数组,该数组被读入字符串类型的响应对象。
<?php
$cars=array("Volvo","BMW","Toyota");
$arrlength=count($cars);
echo json_encode($cars);
exit;
?>
现在我得到的错误是org.json.JSONException: Value Volvo at 0 of type java.lang.String cannot be convert to JSONObject
我认为 thar onSuccess func 接受字符串参数,并且我将 json 作为参数发送给它。这就是导致问题的原因,请帮忙。