java is not running - android

Hi I am new to Android and i am developing one app in which i will send request to server and get json data.The problem is that when receive response fron server and when i trying to get that i get one exception
java.lang.String cannot converted to JSONobject

Your server returned incorrect json-data. There are extra " that should be removed.
It should look similar to
[{
"image": "http://eprintpost.com/images/savepreflab/uploadSave/69120e7d-31e0-46ac-bf07-e86778bc2866.jpg",
"productid": "255",
"oldprice": "250",
"city": "Pune",
"price": "200",
"labname": "Emotions",
"productname": "Sublimation Bag (6058)"
}, {
"image": "http://eprintpost.com/images/savepreflab/uploadSave/48e9420c-3bd2-4c89-91a3-0de3346c072f.jpg",
"productid": "481",
"oldprice": "1000",
"city": "Coimbatore",
"price": "500",
"labname": "savithri photo house",
"productname": "camera toploader 450AW Black"
}, {
"image": "http://eprintpost.com/images/savepreflab/uploadSave/741e6549-94be-439d-b823-0bd8b26bb0ce.jpg",
"productid": "556",
"oldprice": "1000",
"city": "Dharwad",
"price": "400",
"labname": "Foto Magic Studio And Lab",
"productname": "07.jpg"
}]

Could you put all your error log cat ? Where is exactly throw this error
type java.lang.String cannot be converted to JSONObject
Your error is parsing from String to JSONObject so let focus on which lines make a parsing to JSONObject

Related

How to access String name and price from this JSON data?

I have established a connection but unable to access data.Help needed
JSON data
[
{
"name": "Margherita",
"price": 170
},
{
"name": "Corn MAnia",
"price": 170
},
{
"name": "Triple Tango",
"price": 210
}
]
In MarkLogic Server, you can use the XPath language to traverse JSON and retrieve data. Code can be written in the Xquery language or Server Side JavaScript:
xquery version "1.0-ml";
let $doc:= xdmp:unquote('{"root":[{"name": "Margherita","price": 170}, {"name": "Corn MAnia","price": 170}, {"name": "Triple Tango","price": 210}]}')
return xdmp:document-insert('/doc/names.json', $doc)
To get data, use:
doc('/doc/names.json')/root[1]/name
or
doc('/doc/names.json')/root[2]/price/data()

How to change this json object to json array to consume it with Volley?

I am trying to consume below record using JSON with Volley, but it is not a JSONArray, How can I change it to use it in :
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Mean while, The URL in the record seems not correct. How to remove extar forward slashes from url or I can use it to fetch the JPG image?
{
"ID": "131",
"Lead": "",
"Title": "\u06f2\u06f0 \u06af\u0644 \u0641\u0648\u0642 \u0627\u0644\u0639\u0627\u062f\u0647 \u0631\u0648\u0646\u0627\u0644\u062f\u0648 \u062f\u0631 \u0644\u06cc\u06af \u0642\u0647\u0631\u0645\u0627\u0646\u0627\u0646",
"ContentTime": "09:56",
"TypeContent": "public",
"PTime": "1395\/05\/09 - 11:47",
"Content": " <\/div>",
"Tags": "\u0641\u0648\u062a\u0628\u0627\u0644#\u0631\u0648\u0646\u0627\u0644\u062f\u0648#\u0648\u0631\u0632\u0634\u06cc#######",
"Price": "0",
"GID": "106",
"GTitle": "\u0648\u0631\u0632\u0634\u06cc",
"PicURL": "http:\/\/video.dmedia.ir\/images\/news\/131\/thumb_131.jpg",
"comment_count": "0",
"view_count": "0",
"Media": [{
"GID": "359",
"GType": "mp4",
"Title": "",
"URL": "http:\/\/video.dmedia.ir\/images\/news\/131\/media\/359.mp4",
"ADV": 0
}],
"smscontent": null,
"Rels": [{
"RelID": "130",
"RelTitle": " \u0645\u0647\u062f\u0648\u06cc: \u062f\u0648\u0633\u062a \u062f\u0627\u0631\u0645 \u0648\u0627\u0644\u06cc\u0628\u0627\u0644\u0645 \u0628\u0627 \u06cc\u06a9 \u062e\u0627\u0637\u0631\u0647 \u062e\u0648\u0628 \u062a\u0645\u0648\u0645 \u0634\u0648\u062f"
}],
"Comments": []
}
Solution:
We can make JSON object request and JSON array request:
I found this helpful.
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
hi there is one plugin Gson for android studio.You need to install.Then go to CTRL + insert.
You can create gson file.
Enter some name for java file.
Click that file then Paste you json data. Click ok.
You can see your created json to gson format.
thanks hope this will help you.
create model class for this data then use follwing code
ModelClass class=new GSON.fromJSON("your Data ", ModelClass.class )

Android get Facebook User Name from their ID number

Is it possible to get a Facebook user's name from their Facebook Id number with a graph call?
If so, does anyone know the call?
For example you can get a user's picture by making the following call
"http://graph.facebook.com/" + someUserId + "/picture"
Something like:
String response = facebook.request(userId);
JSONObject json = Util.parseJson(response);
String name = json.getString("name");
String username = json.getString("username");
Simply querying the Graph API with the user_id should be enough to get the name of the user -
http://graph.facebook.com/4
>>
{
"id": "4",
"name": "Mark Zuckerberg",
"first_name": "Mark",
"last_name": "Zuckerberg",
"link": "https://www.facebook.com/zuck",
"username": "zuck",
"gender": "male",
"locale": "en_US",
"updated_time": "2012-05-20T01:29:18+0000",
"type": "user"
}
You could drill down even more by adding a filter for the name field -
https://graph.facebook.com/4?fields=name
>>
{
"name": "Mark Zuckerberg",
"id": "4",
"type": "user"
}
https://graph.facebook.com/UserID.
eg:
https://graph.facebook.com/19292868552

How to Parse Facebook Data

I am having a few problems getting Facebook data to parse properly.
I am working on implementing part of an application to allow a user to select and use one of their own facebook photos within the app. I have gotten the facebook login/logout code working and I am currently getting the token (once logged in) in order to gather the users Album information. The permission set is also working nicely, however I am now stuck at trying to get the JSON information to parse correctly. Here is a sample snippet of the information I need to parse:
"data": [
{
"id": "3486732467234",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=434235&id=324343&aid=2430",
"cover_photo": "3489234432",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T23:01:16+0000",
"updated_time": "2011-03-18T19:46:43+0000",
"can_upload": true
},
{
"id": "4043544665",
"from": {
"name": "Persons Name",
"id": "Persons ID"
},
"name": "Vacation 2",
"location": "City",
"link": "https://www.facebook.com/album.php?fbid=4043434665&id=508154335&aid=2555",
"cover_photo": "5434543",
"privacy": "everyone",
"count": 60,
"type": "normal",
"created_time": "2007-06-03T22:53:03+0000",
"updated_time": "2011-03-18T19:45:55+0000",
"can_upload": true
}],
...more paging JSON information ...}}
I need to be able to pull the album information in order to display the photo album names in a list (and use the ID in another query). Once I can get the albums to show, I would then use those IDs to perform another query to get the photos from that album. Again I can also get that information back, I am just not sure how to properly parse it.
Any useful tips that someone might have in being able to parse this into a listview would be greatly appreciated.
I need to target pulling the first ID and the Album name ("id": "3486732467234" and "name": "Vacation" from the first listing)
why don't you try with the json classes...
something like this:
jsonObject = new JSONObject(your_data);
jArray = jsonObject.getJSONArray("data");
for(int i =0;i<jArray.length();i++){
String name= jArray.getJSONObject(i).getString("name");
String location= jArray.getJSONObject(i).getString("location");
etc.
The new Android Facebook SDK 3.0 returns a Response object in the Response.Callback() listener when making Graph API calls. This response object can be used to create a GraphObject which can be used to get a JSON...ex:
Request graphRequest = Request.newGraphPathRequest(session, graphRequestString, new Request.Callback() {
#Override
public void onCompleted(Response response) {
//Create the GraphObject from the response
GraphObject responseGraphObject = response.getGraphObject();
//Create the JSON object
JSONObject json = responseGraphObject.getInnerJSONObject();
}
});

Android: Using GSON to parse a response with dynamic fields

I need to parse a JSON response regarding events in an agenda, of which a field is dynamic. This field changes depending on the month in which the event takes place.
Now I've been parsing data with GSON before, but these all had static fields so it was quite easy to create the necessary POJO's. But how would I do this with dynamic fields?
The response i need to parse is like so:
{
"Agenda/Future": {
"November 2011/0": [
{
"id": "5675",
"eventid": "",
"name": "testing testing 123",
"startdate": "nov 25",
"datecompute": "2011-11-25T08:00:00",
"group_month": "November 2011",
"flg_Data": "Database"
}
],
"February 2012/1": [
{
"id": "5681",
"eventid": "",
"name": "dfd",
"startdate": "feb 3",
"datecompute": "2012-02-03T12:00:00",
"group_month": "February 2012",
"flg_Data": "Database"
},
{
"id": "5679",
"eventid": "",
"name": "vfvd",
"startdate": "feb 17",
"datecompute": "2012-02-17T12:00:00",
"group_month": "February 2012",
"flg_Data": "Database"
}
],
"February 2013/2": [
{
"id": "5680",
"eventid": "",
"name": "df",
"startdate": "feb 14",
"datecompute": "2013-02-14T12:00:00",
"group_month": "February 2013",
"flg_Data": "Database"
}
],
"September 2013/3": [
{
"id": "5677",
"eventid": "",
"name": "fsdfsd",
"startdate": "sep 14",
"datecompute": "2013-09-14T12:00:00",
"group_month": "September 2013",
"flg_Data": "Database"
}
],
"November 2015/4": [
{
"id": "5676",
"eventid": "",
"name": "fsdfsd",
"startdate": "nov 13",
"datecompute": "2015-11-13T12:00:00",
"group_month": "November 2015",
"flg_Data": "Database"
}
]
}
}
As you can see, the object title regarding months is dynamic. Both the value at the end of the title, as well as the title itself change based on the actual month and position in the array of months.
From what I've seen on other questions here at SO, I will need to work with Maps.
But I'm not quite sure how I would go about doing so.
Let's say I create a POJO called Event for the individual events contained in the array of the months, what would my initialization look like?
Regards
I have tried Amir's suggestion by using Jackson with the default Map.
Sadly, this creates a Map with a size of 1. The entire JSON response gets parsed into a single object. Ofcourse, this is not what I want, since I need to get the data from the individual events.
Does anyone have a suggestion as to how I might be able to do that? Normally I'd figure out these things quite quickly but I can't wrap my head around this one due to the dynamic object naming.
I have eventually managed to crack this problem. I ended up using plain JSONObjects, which worked eventually.
The code I used to get it to work is as follows:
JSONObject jsonObject = new JSONObject(response);
JSONObject agenda = jsonObject.getJSONObject("Agenda/Future");
if (agenda != null) {
System.out.println(agenda.length());
JSONArray events = agenda.names();
if (events.length() > 0) {
System.out.println("At least its bigger then 0. It's: "
+ events.length());
for (int i = 0; i < events.length(); i++) {
System.out.println(events.get(i).toString());
JSONArray test = agenda.getJSONArray(events.get(i)
.toString());
if (test != null) {
JSONObject testt = test.getJSONObject(0);
if (testt != null) {
System.out.println(testt.getString("name"));
} else {
System.out.println("Still empty, Check again.");
}
}
}
}
} else {
System.out.println("Agenda is completely empty, try again.");
}
As you can see, this still contains test-names, but at least it works.
Thanks for the help everyone!
Since the month data are not valid java identifiers, you 'll probably have to use custom serialization with GSON. You're talking about using Map objects but if you are going to do that you may as well us the org.json objects like JSONObject which are basically built on maps/hashtables anyway.
If all your fields, as you have said, are dynamic then you can create a super set of all fields in a pojo. But that doesn't really help and I think the Map is the best solution. With GSON, you can do
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> map = gson.fromJson("{'key1':'123','key2':'456'}", type);
This will return a map of strings.
I haven't actually used GSON to do this before though. I have been very happy with Jackson lib and with that you can do
new ObjectMapper().readValue("...json...", Map.class)
The above will return a map of maps which is what you want.

Categories

Resources