I'm working on android project I received JSON Object
{"t":"Terminal1.mobileWalletPassword","v":"0000"},{"t":"Terminal1.PasswordAdjust","v":"0000"},{"t":"Terminal1.PasswordAuth","v":"0000"}
{"t":"Terminal1.Acquirer4.Issuer1.BinList1.BinRange61.txtMatchLength","v":"00"},{"t":"Terminal1.Acquirer4.Issuer1.BinList1.BinRange61.txtName","v":"CUP_620220"}
in my project there are classes like
public class Terminal {
private String PasswordAuth="";
private String mobileWalletPassword
....
}
and I have
public class Acquirer{
.....
}
and I have
public class Issuer{
...
}
How I can handle this response
It is possible to send the full output format of the api .because your file is not valid ! ?
Your response not possible to cast to your model.
Related
I want to use T in Retrofit, but I do not know, how to set T in Retrofit.create(X.class) method.
public interface SupportService<T> {
#GET
Flowable<BaseResponse<T>> getApi(#Url String url, #Query("data") JsonObject jsonObject);
}
HttpRetrofit.getInstance()
.getRetrofit(this)
.<SupportService<T>>create(SupportService.class)
.getApi("",null)
public class BaseResponse<T>{
public int code;
public String message;
public T data;
}
"T" stays for a generic type of your choice. In retrofit, you can define your data model class accordingly to api response, for example with #SerializedName annotation if you are using GSON as serializing library. That "T" type will be the class defined for the api response. Here you can find a simple starting point tutorial about retrofit.
First of all, i'm using retrofit with gson and autovalue-gson extension
Check here : https://github.com/rharter/auto-value-gson
In API i have a different models, where API returns json with id, description etc and also array of messages, but these messages may vary depending on what type of social media i want to (facebook, twitter, instagram etc...). So i made model like this :
#AutoValue
public abstract class SocialStream<T> {
public abstract int id();
......
......
public abstract List<T> messages();
public static <T> TypeAdapter<SocialStream<T>> typeAdapter(Gson gson, TypeToken<? extends SocialStream<T>> typeToken) {
return new AutoValue_SocialStream.GsonTypeAdapter(gson, typeToken);
}
}
As you can see i also passed typetoken to AutoValue typeAdapter, and everything seems to work, i already logged whole json, but problem occurs when i try to use it as my model.
SocialStream<FacebookPost> post = getList().get(0);
method getList() returns array of these messages and this is the line where i'm getting exception:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.model.FacebookPost
at
I have exactly the same problem like me. The key is creating a customized deserializer as Gson has no idea about the real class inside.
try this
https://stackoverflow.com/a/34685986/2458190
I am new to android development. I am trying to access json data in android. I have two tabs with viewpager . First tab contains information about events and the second tab will have a custom listview having a list of students attending that event.
this is the link to my json -https://jsonblob.com/5695f83de4b01190df49255d
I am confused on how to access this data in each fragment. My first screen has a edittext where the user will enter the event id.
Here is the link of design
https://drive.google.com/file/d/0B5XX8x3YUUrqYWUxNGdyckVaQ00/view?usp=sharing
I have developed the interface . Can anyone please help me with implementing the functionality.
thank you.
#Lalit Pratap Singh's answer is correct.
And i post my way below:
That's the answer.
http://7xphy5.com1.z0.glb.clouddn.com/A28B70BF-31C1-48F4-9BF9-61DB197A864B.png
1.Correct your url according my comment. http://jsonblob.com/api/jsonBlob/5695f83de4b01190df49255d
2.Get the response use APIKitchen or search the url on the internet and then you will get the response like this. Copy all the content.
http://7xphy5.com1.z0.glb.clouddn.com/A015706D-59E9-4E6C-B855-30B1152D30B6.png
3.Use a plugin in Android Studio called GsonFormat. Paste the content to plugin and it will create a new Events.java for you.
4.Use Retrofit to get the RESTFul Data.
I didn't paste my code here. You can leave your e-mail and i'll send code and detail explanatioin to you.
Create Application class in your code.
public class MyApplication extends android.app.Application {
public static MyApplication mInstance;
public String jsonStr;
#Override
public void onCreate(){
super.onCreate();
mInstance = this;
}
public MyApplication getmInstance(){
return mInstance;
}
public void setJsonStr(String jsonStr){
this.jsonStr = jsonStr;
}
public String getJsonStr(){
return jsonStr;
}
}
Add application class in you manifest file
android:name=".MyApplication"
Fetch json data and set it to application class like from anywhere in you application
MyApplication.getmInstance().setJsonStr(jsonstr);//fetched json data from httpcall
Get all data from Runtime and do any operation on it
String jsonStr = MyApplication.getmInstance().getJsonStr();
I have the following code which produces the error: Error:Parceler: Unable to find read/write generator for type io.realm.Realm for io.realm.RealmObject.realm
It was working all fine without extends RealmObject , however I want to use Realm to put to database easily. Is there a way to exlcude the RealmObject fields and just use the basic pojo fields for #Parcel?
#Parcel
public class Feed extends RealmObject{
int id;
public String text;
public String time_created;
String time_modified;
int comments_count;
int likes_count;
String feed_type;
int obj_id;
String image;
String user_name;
String user_earthmile_points;
boolean liked;
boolean commented;
boolean is_private;
String url;
int feed_creator_id;
}
EDIT #2: Actually, I found a way to make it work :). See the updated answer below.
EDIT #1: While the app compiles just fine, it crashes when trying to actually create a Parcel with the error: org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy. The Realm team has officially acknowledged that it is currently not possible to implement Parcelable on RealmObjects. It is unclear if / when this will be resolved.
With Parceler v0.2.16, you can do this:
#RealmClass // required if using JDK 1.6 (unrelated to Parceler issue)
#Parcel(value = Parcel.Serialization.BEAN, analyze = { Feed.class })
public class Feed extends RealmObject {
// ...
}
Then, use Parcels.wrap(Feed.class, feed) instead of Parcels.wrap(feed) everywhere, otherwise your app will crash with org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy.
All classes that extend RealmObject will have a matching RealmProxy class created by the annotation processor. Parceler must be made aware of this class. Note that the class is not available until the project has been compiled at least once.
#Parcel(implementations = { PersonRealmProxy.class },
value = Parcel.Serialization.BEAN,
analyze = { Person.class })
public class Person extends RealmObject {
// ...}
Iam facing with XmlGregorianCalendar convertion issue on android 2.3.X application project
iam building an android 2.3.X application that interact with Rest Services. Some objects provided by thes services have XMLGregorianCalendar datatype . these object classes have been generate throuth JAXB Binding.
on the android side i got the solution on using Gson to deserialise an serialise XMLGregorianCalendar datatype by this code
public class XMLGregorianCalendarConverter {
public static class Serializer implements JsonSerializer{
public Serializer(){
super();
}
#Override
public JsonElement serialize(Object t, Type type, JsonSerializationContext context) {
XMLGregorianCalendar xgcal = (XMLGregorianCalendar) t;
return new JsonPrimitive(xgcal.toXMLFormat());
}
}
public static class Deserializer implements JsonDeserializer{
#Override
public Object deserialize(JsonElement t, Type type, JsonDeserializationContext context) {
try
{
return DatatypeFactory.newInstance().newXMLGregorianCalendar(t.getAsString());
}
catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
this code worked fine in java desktop environment. but in android i got some Logcat saying "xerces.jaxp.datatype.DataTypeFactoryImpl not found".
I tryed to include xerces from apache as mentioned in one workaround for this kind of problem in this forum. but i got some dalvik error and i was unable to build my project.
please i need help on how to fix this. I dont want to change anything on the Rest side.
If you use proguard, then you can try to add this to the proguard.txt file:
-dontwarn org.apache.xml.resolver.**
-dontwarn org.xml.sax.**