I am trying to upload a file using HttpPost
and send it to php
where I would get a response in JsonArray
. However the problem I am having is to use the JsonArray
or object to use in android app. The information from the array will then be put in to SQLite
db with in the app. 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");
I am not well verse with HttpPost
and would appreciate any detail help I can get. I have tried few things from net but was not successful.
Array
(
[response] => Array
(
[Filepathserver] => webaddress
[email] => xyz@email.com
[syncstatus] => yes
)
)
java file
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
$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)
your php file produced a json object, not a json array.
so, try JSONObject arr =object.getJSONObject("response");
By the way, your logcat had already reminded you to treat it as a JSONObject.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.