[英]at response of type org.json.JSONObject cannot be converted to JSONArray
I am trying to upload a file using HttpPost
and send it to php
where I would get a response in JsonArray
. 我正在尝试使用
HttpPost
上传文件并将其发送到php
,在JsonArray
中将得到响应。 However the problem I am having is to use the JsonArray
or object to use in android app. 但是我遇到的问题是在Android应用程序中使用
JsonArray
或对象。 The information from the array will then be put in to SQLite
db with in the app. 然后,来自数组的信息将与应用程序一起放入
SQLite
db中。 I am unable to extract the data from array and getting the following error 我无法从数组中提取数据并得到以下错误
"at response of type org.json.JSONObject cannot be converted to JSONArray" on JSONArray arr =object.getJSONArray("response");
JSONArray上的“响应类型为org.json.JSONObject的响应无法转换为JSONArray” arr = object.getJSONArray(“ response”);
I am not well verse with HttpPost
and would appreciate any detail help I can get. 我对
HttpPost
不太熟悉,希望得到我提供的任何详细帮助。 I have tried few things from net but was not successful. 我从网上尝试了几件事,但没有成功。
Array
(
[response] => Array
(
[Filepathserver] => webaddress
[email] => xyz@email.com
[syncstatus] => yes
)
)
java file Java文件
String url = "uploadphpfile.php";
File file = new File(filepath);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(mpEntity);
Log.d("enitity",mpEntity.toString());
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
String result = EntityUtils.toString(r_entity);
JSONObject object = new JSONObject(result);
JSONArray arr =object.getJSONArray("response");
for (int i = 0; i < arr.length(); i++) {
JSONObject obj = arr.getJSONObject(i);
Log.d("filepathserver",obj.get("Filepathserver").toString());
}
//Do something with response...
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
String responseString = EntityUtils.toString(r_entity);
} else {
String responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (Exception e) {
Log.e(DBHelperReports.class.getName(), e.getLocalizedMessage(),
e);
}
}
php file php文件
$response = array();
$response["Filepathserver"] = $target_path;
$response["email"] = $target_path;
$response["syncstatus"] = 'yes';
echo json_encode(array("response"=>$response));
Logcat logcat的
Value
{"Filepathserver":"webaddress","email":"xyz@email.com","syncstatus":"yes"}
at response of type org.json.JSONObject cannot be converted to JSONArray
org.json.JSONException: Value
{"Filepathserver":"webaddress","email":"xyz@email.com","syncstatus":"yes"}
at response of type org.json.JSONObject cannot be converted to JSONArray
at org.json.JSON.typeMismatch(JSON.java:100)
at org.json.JSONObject.getJSONArray(JSONObject.java:588)
at com.multiinspectionelite.DBHelperReports$1.run(DBHelperReports.java:383)
at java.lang.Thread.run(Thread.java:762)
your php file produced a json object, not a json array. 您的php文件产生了一个json对象,而不是json数组。
so, try JSONObject arr =object.getJSONObject("response");
因此,请尝试
JSONObject arr =object.getJSONObject("response");
By the way, your logcat had already reminded you to treat it as a JSONObject. 顺便说一句,您的logcat已经提醒您将其视为JSONObject。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.