I am using this code in my PHP script:
echo json_encode($output);
and I get the following in my pages output:
[{"food_id":"1","food_description":"Free Range Egg","food_protein":"8"},{"food_id":"3","food_description":"Seeded Bread","food_protein":"6"}]
Can you please lead me the way on how I can receive the pages output in my Android Application and fit in in my object below? e.g.:
food.setFood_id(c.getInt("food_id"));
You need to do something like this..
JSONObject jsonObject = new JSONObject(response);// response is your json data which you are sending from php
JSONArray jsonArray = jsonObject.getJSONArray("mMessage");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
food.setFood_id(jsonObj.getInt("food_id"));
}
and from php side you need to send data like this
public function get_data()
{
$json_response = array();
$sql_querry_arry = array(); // array filled with sql result
$json_response['mMessage'] = $sql_querry_arry;
echo json_encode($json_response);
}
Now receive output as a string and follow the steps i mention before
Related
I have a .php file on the server side that works fine and extracts me results with PDO method. Therefore, when I click the file on the browser, I have no problem.
Yet, I would like this output to be put in a textView in my Android app.
Now, by the end of the php file i added:
print(json_encode(array($output)));
while in MainActivity.class I tried to get this output by parsing it through JSON.
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
}
TextView.setText(s);
Now, how do I end the JSONObject, so that "s" becomes a String that can later be added to the TextView?
Thanks in advance
Try this.
String s="";
JSONArray jArray = result.getJSONArray(result);
for(int i=0 ; i < jArray.length() ; i++) {
JSONObject json = jArray.getJSONObject(i);
s = s+"Price : "+json.getString("Price");
}
TextView.setText(s);
I'm having difficult with an error I keep getting when trying to process my JSON.
Value [] of type org.json.JSONArray cannot be converted to JSONObject is an error I keep getting and I don't know whats causing it.
The JSON is:
{
"recipe":[
{
"recipeID":"1",
"recipeName":"chicken salad",
"ingredients":"chicken lettuce tomato cucumber"
},
{
"recipeID":"2",
"recipeName":"banana shake",
"ingredients":"banana ice cream vanilla "
}
]
}
And what I'm doing to process it is:
JSONArray recipes = null;
try {
JSONObject jsonObj = new JSONObject(json);
recipes = jsonObj.getJSONArray("recipe");
HashMap<String, String> map = new HashMap<String, String>();
// looping through All recipes
for (int i = 0; i < recipes.length(); i++) {
JSONObject c = recipes.getJSONObject(i);
name = c.getString("recipeName");
}
But I keep getting this error :/
I see you are parsing the Json string by hand, you can do it, and it is quickly enough, but in my case I always use some type of library that do the job for me, so I will like to recommend you that take a look at Gson library from google.
https://code.google.com/p/google-gson/
Hi everyone I have this php output.
//fetching all the rows from the query
$row = $stmt->fetchAll();
//output to json
die(json_encode($row));
[{"u_id":"8","id":"8","name":"test1","location_long":"10.01475","location_lati":"53.57823","description":"loo\nlol","date":"2014-11-21","time":"18:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},
{"u_id":"8","id":"9","name":"test9","location_long":"10.01475","location_lati":"53.57823","description":"test9\n","date":"2014-11-21","time":"19:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},
{"u_id":"8","id":"10","name":"test10","location_long":"10.01475","location_lati":"53.57823","description":"lol","date":"2014-11-22","time":"19:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},
{"u_id":"8","id":"11","name":"lol","location_long":"10.01475","location_lati":"53.57823","description":"","date":"2014-11-24","time":"18:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"}]
Now I want get each item with its value into my listview item
Code:
JSONObject json = jsonParser.makeHttpRequest(
EVENTS_URL, "POST", params);
JSONArray jsonArray = new JSONArray(json);
for(int i = 0; i < jsonArray.length();i++)
{
adapter.add(new DataProvider(own_event,
jsonArray.getJSONObject(i).getString("description"),
jsonArray.getJSONObject(i).getString("name"),
jsonArray.getJSONObject(i).getString("date"),
jsonArray.getJSONObject(i).getString("time"),
jsonArray.getJSONObject(i).getDouble("location_long"),
jsonArray.getJSONObject(i).getDouble("location_lati"),
jsonArray.getJSONObject(i).getInt("id")));
}
Now I get this error:
Error parsing data org.json.JSONException: Value [] of type
org.json.JSONArray cannot be converted to JSONObject
In the line where I create the JSONArray jsonArray.
I never worked with json before so, is there a better way to parse the data from my php file to android, and a more secure way in doing that?
Server response is came in JSONArray so no need to convert in JSONObject just build JSONArray from server response and return as JSONArray instead JSONObject from makeHttpRequest :
JSONArray jsonArray = jsonParser.makeHttpRequest(EVENTS_URL, "POST", params);
Android cannot get data when I include function.php file.But Android get data when I remove file include code in current php file.How to solve this error.I wanna use some function in function.php file.So I must use include code to call function.php.
I've no idea.is it android or php fault?
My PHP version : 5.3.10
apache version : 2.2.21
My android code:
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 500000);
HttpConnectionParams.setSoTimeout(client.getParams(), 500000);
HttpUriRequest request = new HttpGet(CategoryAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null){
str += line;
}
// parse json data and store into arraylist variables
JSONObject json = new JSONObject(str);
JSONArray data = json.getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject object = data.getJSONObject(i);
JSONObject feed = object.getJSONObject("Feeds");
FeedItem.allFeedItemsList.add(new FeedItem(Integer.parseInt(feed.getString("id")), feed.getString("name"), feed.getString("post_img"), feed.getString("post_description"), feed.getString("userimage"), feed.getString("post_date"), feed.getString("post_title")));
}
Anyone can help??????????
Now i can solve my error by Encoding in ANSI. Since I Encode in UTF-8, android can't load. Now, It is fine and ok. Thanks.......
Try using this library:-https://github.com/Abhishekpalodath/FetchServerUtil-Android.git.
You can also load real-time data using this.
In my Android app, I'm trying to communicate with a web server that holds randomly-generated fake usernames and scores.
{ scores: [
{
un: "Feltricapulta",
sc: "143"
},
{
un: "Henroid",
sc: "120"
},
{
un: "ieteubmospta",
sc: "70"
},
{
un: "pmbotesteuai",
sc: "67"
},
{
un: "epesomiubtat",
sc: "65"
}
] }
The code in the PHP file looks like this:
<?php
include ('connecttomysql.php');
$command = 'SELECT un, sc FROM xmlscores ORDER BY sc DESC';
$execute_command = mysql_query($command);
echo '{ "scores": ';
while ($table_row = mysql_fetch_assoc($execute_command))
{
$jsonArray [] = $table_row;
}
echo json_encode($jsonArray);
echo '}'
?>
I've called this .php URL in Android using the generic HttpGet method. The output of the json data prints to the stacktrace and provides me with the "Cannot convert from Object to Array" error.
Looking at the PHP file and the json output, is there anything noticeably wrong with my codes or the output? I can't figure it out.
"Cannot convert from Object to Array"
means you are trying to convert response string to JSONArray. but Current String Contains JSONObject as root element instead of JSONArray. so convert it to JSONObject as:
JSONObject json=new JSONObject(<Server response string here>);
// get scores JSONArray from json
JSONArray jsonscoresarray=json.getJSONArray("scores");
...
// Implementation of Json array should go like this.
#Override
public void fromJSON(Object json) throws JSONException {
JSONObject jsonObject = new JSONObject((String) json);
JSONArray jsonArray = jsonObject.getJSONArray("scores");
mScores = new ArrayList<Score>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObjct = (JSONObject)jsonArray.getJSONObject(i);
Score object = new Score();
object.fromJSON(jsonObjct);
mScores.add(object);
}
}