【发布时间】:2017-12-21 01:36:46
【问题描述】:
我正在尝试使用HttpPost 上传文件并将其发送到php,在那里我会在JsonArray 中得到回复。但是我遇到的问题是使用JsonArray 或对象在android 应用程序中使用。然后将数组中的信息放入应用程序中的SQLite db。我无法从数组中提取数据并出现以下错误
" org.json.JSONObject 类型的响应无法转换为 JSONArray" on JSONArray arr =object.getJSONArray("response");
我不太了解HttpPost,如果我能得到任何详细的帮助,我将不胜感激。我从网上尝试了一些东西,但没有成功。
Array
(
[response] => Array
(
[Filepathserver] => webaddress
[email] => xyz@email.com
[syncstatus] => yes
)
)
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 文件
$response = array();
$response["Filepathserver"] = $target_path;
$response["email"] = $target_path;
$response["syncstatus"] = 'yes';
echo json_encode(array("response"=>$response));
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)
【问题讨论】:
-
类型不匹配错误。
标签: java php android json http-post