Retrieve ID of JSON when POST in Firebase, Android - android

I POST a JSON into my Firebase Database, using okHttp. Then I retrieve a response and I do:
response.body().string();
But when I print this, instead of a JSON I get:
/**/ gotData({"name":"my_object_ID"});
I want to extract the JSON inside of that String.

Related

How do I construct and pass raw json data with okHttpClient in android

I am using a query where I need to POST to an endpoint with okhttpclient.
The issue is I have the payload value and the senttime. I just don't know how to create a json object to send over as a raw body object to the endpoint.
To make things more clear:
here's my postman screenshot:
enter image description here
This is my post request :
https://example.org/api/service-profile/v1/device-notification/{deviceId}
Now I am planning to pass {deviceId} in the path, authorization token in the header and finally , as raw payload query like this in the body :
{
"payload": {"notificationID":"677291f5-a784-43df-af2e-0363a4067e9c","title":"test 223","body":"payload","lockScreenVisibility":1,"groupMessage":"","fromProjectNumber":"819539062812","priority":5,"rawPayload":"{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1591849563191,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"i\\\":\\\"677291f5-a784-43df-af2e-0363a4067e9c\\\"}\",\"oth_chnl\":\"\",\"pri\":\"5\",\"abc\":\"1\",\"from\":\"819539062812\",\"alert\":\"payload\",\"title\":\"test 223\",\"grp_msg\":\"\",\"google.message_id\":\"0:1591849563205759%409ebbcaf9fd7ecd\",\"google.c.sender.id\":\"819539062812\",\"notificationId\":-1451117355}"}
,
"sentTime": 1591849563191
}
trouble is I have the payload value and senttime, but I dont know how to construct a json object such that i can create the structure as { "payload" :pass in my payload value, "senttime":pass in sent time}
and then pass it as a raw body param over okhttpclient? Any idea how to accomplish this?
Thanks!

How to parse single object from json

I am working on an Android project where I am using volley to parse the data from JSON. I am able to parse the data if it is an array by following this tutorial. But when I try to parse a single object using getJSONObject, it is returning null. I want to get the value of that particular object.
This is my JSON file:
In the above file I want to retrieve only responseInfo which is a JSON object.
You need to create JSONObject
JSONObject object = new JSONObject(responce);
//get responce code
String query = object.getString("responceCode");
Please use Gson library for json parsing
https://github.com/google/gson

How to parse data in Json farmet using google http client (Android app)?

I am getting data from server by building a get request (HttpRequest). The data is not in Json format (when I open the link in a web browser, it says "This XML file does not appear to have any style information associated with it. The document tree is shown below.")
Since it is the case, when I call this line:
HttpRequest request = buildGetRequest("TreeLocation");
final LocationListResponse response = request.execute().parseAs(getResultType());
The app stops there, and I think it is because it does not regconize the resultType.
So, now I want to declare the content type as Json. Anyone knows how to declare the content type in Google http client library?
just convert your xml response into json using json library..and then you will get json response
use this link Convert XML to JSON object in Android
Edit 1:
you can add this and try
httpPost.setHeader("Content-type", "application/json");
for json respomse

How to remove the special characters when getting the data using json in android?

I am working on android application. I need to get the data from PHP web service. The response which I am getting from PHP server is shown below:
String response = [{id:100,category:local,count1:58,count2:86},
{id:101,category:server/local,count1:18,count2:27},
{id:102,category:server & local,count1:19,count2:28}];
But I am getting the following message when I am trying to separate the data using JSON.
org.json.JSONException: Unterminated object at character
How to remove special characters and get the data using JSON array?
Strings should be under double quotes. Your json is not a valid response. So change your response from
{id:100,category:local,count1:58,count2:86}
to
{
"id":100,
"category":"local",
"count1":58,
"count2":86
}
Refer here
Valid JSON format
{
"Status":"OK",
"Error":{
"Code":0,
"Description":""
},
"Data":[]
}

Posting JSON from Android to php page

Need to post JSON data to the server , i have valid json object.
My problem is that, {"code":"400","message":"Failed loading JSON. Special characters must not be included in the request. Please check the requested JSON."}
This is my URL :- http://www.invoicera.com/app/api/check_json_api.php?token=7B92C122473A3D6F54E60D20AC5526D0
And this is my JSON Data which i want to post :- { "listInvoice" : {
"client_id":"",
"date_from":"",
"date_to":"",
"invoice_number":"",
"invoice_record_status":"Draft",
"invoice_status":"Active",
"page":"1",
"per_page_record":"10"
}
}

Categories

Resources