How to get all the parameters in following response in Android - android

I want to get parameters from this response !!
Bundle[{custom={"custom data":{"notification_type":"offer","offer_id":4348}}, from=1013970362419, badge=1, message=birds view, android.support.content.wakelockid=4, collapse_key=do_not_collapse}]
I want offer_id and notification_id , in above notification data

As the response is received in Bundle, you cannot parse it directly. Instead you can get each parameter of bundle using bundle.getString(key) or bundle.getInt(key) whatever type of data is.
So in your response to get offer_id, first of all extract custom parameter as a string and convert it into JSONObject. Then you will be able to get offer_id.
E.g.
JSONObject keyData = new JSONObject(data.getString("custom"));
Gson gson = new Gson();
KeyValueModel keyValue= gson.fromJson(keyData.toString(), KeyValueModel .class);
String offerId = keyValue.getString("offer_id")

Related

When posting the JsonArray in retrofit automatically add some okhttp content in json

I need some help to pass the jsonarray in retrofit post method in Android.
I have create one pojo class List<ReportList> when I call the api that time i convert this pojo class into JSONArray using Gson() like this
JsonArray myCustomArray = new Gson().toJsonTree(ReportsArray).getAsJsonArray();
and than I post this jsonArray into retrofit post method like this
#FormUrlEncoded
#POST(midURL)
Call<Json> ReportsManagement(#Field(fClass) String classs,
#Field(fMethod) String method,
#Field(fType) String type,
#Field(fUserID) String userID,
#Field(fDate) String Date,
#Field(fReportsArray) String ReportsArray,
#Field(fEntryByUser) String EntryByUser);
Now when I call the api that time I have show some issue in passing the json data in api in android studio logcat.
class=crud&method=ReportsManagement&Type=ADD&UserID=8&Date=2018-06-08&ReportsArray=[{"Date":"08-06-2018","IV1":"","IV10":"","IV11":"","IV12":"","IV13":"","IV14":"","IV15":"","IV16":"","IV17":"","IV18":"","IV19":"","IV2":"","IV20":"","IV21":"","IV22":"","IV23":"","IV24":"","IV25":"","IV26":"","IV3":"","IV4":"","IV5":"","IV6":"","IV7":"","IV8":"","IV9":"","IVF":""},{"Date":"08-06-2018","FIO2":"","IV1":"","IV10"%06-08 08:13:07.461 23291-23476/com.abc D/OkHttp:3A"","IV11":"","IV12":"","IV13":"","IV14":"","IV15":"","IV16":"","IV17":"","IV18":"","IV19":"","IV2":"","IV20":"","IV21":"","IV22":"","IV23":"","IV24":"","IV25":"","IV26":"","IV3":"","IV4":"","IV5":"","IV6":"","IV7":"","IV8":"","IV9":"","IVF":""}]
Error in json :%06-08 08:13:07.461 23291-23476/com.abc D/OkHttp:3A

how to send this JSON Array from android to server using retrofit

I want to send this parameter in request and tried to send a simple array list but that was not working
"Language": [
"string","string","string"
]
If "Language" is key and ["string1","string2","string3"] is value then create method in interface as below.
#FormUrlEncoded
#POST(LINK_API)
Call<ResponseModel> getResponse(#Field("Language") String languageArray);
and call it using interface instance like this :
JSONArray languageArray = new JSONArray();
languageArray.add("string1");
languageArray.add("string2");
languageArray.add("string3");
String langArray = languageArray.toString();
Call<ResponseModel> responseModel = apiObject.getResponse(langArray);
responseModel.enqueue(...);
This will work perfectly.
try post query with field parameter as string..
convert your jsonObject to String using,
String b = json_object.toString();
At Server Side, convert string back to JsonObject or whatever else you need.

How to send list of byte arrays to json

How i can pass ArrayList<byte[]> to JSONObject as a parameter?
For example, I need to send a Id (string), Name (string) and photoList (ArrayList<byte[]>) in a json object.
You can use GSON LIBRARY . The code you need can be as :
String jso;
jso = new Gson().toJson(photoList);
It should convert the arraylist(i.e. photoList) to jsonObject.
Just use GSONlib for this if you want to convert arraylist into json
If you are using GSON library use the following code to convert your ArrayList to JSON.
String jsonString = new Gson().toJson(yourArrayList);

How to extract objects from json

I have a scenario where I need only 1 objects out of the entire json.
{"id":"1","first_name":"Steve","last_name":"Holt","user_type":"Teacher","user_key_area":"Math"}
In above I want to extract user_type.
How will I do?
You can use Google GSON to parse the json response and map it to the model directly . Here is the tutorial for the same TUTORIAL
Use this to extract the user_type from the json, pass the your json response in place of response variable.
JsonObject object =new JsonObject(response);
String user_type = object.getString("user_type");
You are getting this json in response you can get 1 object like this:
String value = response.getString("Key Name");
Get Response and that create JsonObject
JsonObject jsonObject =new JsonObject(res);
Create String object and pass string parameter "user_type"
String userType = jsonObject.getString("user_type");

How to retrieve key/value array from Bundle on Android

I'm trying to retrieve data from "extra" key:
Bundle[
{from=1058706545539,
extra={
"ty":"msg",
"d":"sec":1425242647,"usec":763000},
"iL":"86777e87a574c3f068f6525e",
"tU":"7e0a9dbbd1d6ee1795d64fdf",
"iP":"4f26e5f78d042e2224688ed7",
"iM":"dd83db95e764b103b4fec99e"},
message=Oi ,
android.support.content.wakelockid=1,
collapse_key=do_not_collapse
}]
If it was JSon I'd use JSONObject, I don't know how to retrieve the the whole "extra" on a HashMap structure. So that I can use something like that:
String ty = extra.getString("ty");
I receive this bundle from Push Notification.
You can get the string, then make it a JSON Object:
String json = extra.getStringExtra("extra");
Then:
JSONObject jobject = new JSONObject(json);

Categories

Resources