我有一个非常简单的PHP代码:
<?php
$myArr = array("John", "Mary", "Peter", "Sally");
echo json_encode($myArr);
?>
在我的android工作室中我有以下asynctask类,它将产生一个名为result的字符串:
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
result = HttpManager.SendData(dataPack);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
Toast.makeText(c,result,Toast.LENGTH_SHORT).show();
mAuthTask = null;
}
@Override
protected void onCancelled() {
mAuthTask = null;
}
}
使用结果字符串类的jsonparser如下:
private String[] jSonParser(String s){
String[] mlist={""};
mlist[0]="no users found";
if (s != null) {
try {
JSONObject o = new JSONObject(result);
JSONArray a = o.getJSONArray("types");
for (int i = 0; i < a.length(); i++) {
mlist[i]=a.getString(i);
}
Toast.makeText(c, "JSON recieved!", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(c, "JSONException", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(c, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
}
return mlist;
}
但我一直得到JSONException。我做错了什么?