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();
Related
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.
I would like to implement a service(Web service) call in my application.
I blocked here for a while.
Previously I followed some of the below concepts.
like, AsyncTask class, Thread concepts and Handlers.
Recently I heard about the Retrofit.
Based on by experience, retrofit was good with high performance.
But it's not that much of reliable.
Example:
{
"Tag1":"Tag Value",
"TagArray":[ {"key1":"value","key2":"value"},{"key1":"value","key2":"value"},{"key1":"value","key2":"value"} ]
}
POJO:
public class Data{
String key1,key2;
sterres...
getters..
}
If the response have some other tags that are no need and the inside data only we need in our app i,e. "TagArray".
I need the handle only this response.
In such type of case this retrofit was failed.
Is there any other libraries or any other components to implement service calls in android with high performance are existed.
You can add your POJOs' or Beans' fields #Optional. This option comes from GSON that used in Retrofit as default.
EDIT :
public class ExamplePojo implements Serializable {
#SerializedName("TagArray")
public ArrayList<Keys> TagArray;
public ExamplePojo() {
TagArray = new ArrayList<>();
}
public static class Keys{
#SerializedName("key1")
public String key1;
#SerializedName("key2")
public String key2;
}
}
The attributes which you ignore shouldnt be added on POJO class or as mention above, add optional annotation if it can be null.
I have two projects, let's call them App and Dependency.
Dependency is a some kind of GCM parser project that does ... things. It's hard to tell, because it's poorly written, company project that I am forced to use.
Anyways, I'm currently trying to change the part, where I show the notification. I wan't to filter that because sometimes I get notifications that were already showed. My plan is co compare ID's with GreenDAO database... which is implemented inside App. I have singleton class-manager for my database, that manages all database queries.
How should I get to that class from dependency project?
I know, that I should use interfaces, one way or another, but I'm not sure how.
I don't know how your classes are, but in general, you could do something like this:
In Dependency project:
Create a listener for receiving the list you want from the database, let's say onDBRequest:
public interface onDBRequest {
public List<Integer> onIDRequest();
}
Then, in your class where you show he notifications you can do:
public class ClassThatShowsNotifications {
private OnDBRequest mDbListener;
public onDBRequest getDblistener();
public void setDblistener();
...
}
When you need the list of IDs to compare , you can just call:
getDbListener().onIDrequest(); //will return the list of IDs you need to compare
In App project:
Make your DAO implement onDBRequest and implement onIDRequest to return the list you need to compare later:
public class YourDAO implements onDBRequest{
...
public List<Integer> onIDRequest(){
List<Integer> IDs;
//do your stuff to return the IDs
return IDs;
}
}
Now, the only thing you need to do is set the listener you want on the class that shows notifications. Example:
In the YourDAO class
ClassThatShowsNotification notifications = new ClassThatShowsNotifications();
notifications.setDBListener(this);
EDIT
If your class is a based service class, you can do pretty much the same. When you start the service you can can pass your singleton as the listener. You can work with bindService() or make a singleton from your service.
public class YourService extends Service{
private static YourService sInstance;
public static YourService getInstance(){
return sInstance;
}
onCreate(){
sInstance = this;
}
}
Then, when you start the service
YourService.getInstance().setDBListener((onDBRequest)YourDAOSingleton.getInstance());
This would work fine since you have a local service, but you can also take a look at Broadcast Receivers
I am trying to do something like that , Firstly , there is a MainActivity which stores courses in ArrayList and these courses are showed by listview.After that , I pass to another activity which is called as ShowDetailActivity with startActivity() to show course details when I click list view element.Then, I pass to another activity which is called EditCourseActivity to edit course which is shown by ShowDetailActivity. The problem is that When I want to edit course , I have to access MainActivity 's Arraylist (private) but I cannot pass with startActivity() because MainActivity did restart (reinstalled) that's why there is no courses on arraylist. The question is How can access from EditCourseActivity to MainActivity 's arraylist to show updated courses ?
Store the course information in a singleton class
public class CourseHolder{
public static CourseHolder instance=null;
public static CourseHolder getInstance(){
if(instance==null){
instance=new CourseHolder();
}
return instance;
}
private Course courses[];
private CourseHolder(){
courses=new Course[10];
}
public void setCourse(int index, Course course){
courses[index]=course;
}
public Course[] getCourses(){
return courses;
}
}
In the EditCourseActivity, after editing, store the updated course information in the copy of CourseHolder
In MainActivity.onResume(), call listView.setAdapter(null), then refresh it with the data inside the CourseHolder
As a generic answer: detach the data from its representation. You can achieve this in many ways:
Singleton wrapper for your array list
To be android specific: wrap your array in a ContentProvider
Use Android SDK SQL database support
I want to send an object between Activities. Reading up on this i have come to conclusion that using Parcelable is the best way to go as it has the least performance impacts. The reason i want to do this is because i need to download data from the network to create an Object, so i don't want to keep downloading the data.
However, in order to use a Parcel the class needs to implement Parcelable. As i am trying to send an Object defined in a Library i cannot edit the class to include this.
What would be the best way to solve my predicament?
I have tried extending the library class and implementing Parcelable but failed with a ClassCastException. I have also seen mentioned that i should make a Parcelable class that wraps around my library class and send it that way?
Thanks!
How about using the parceler library? You can tell your Application to parcel library classes:
#ParcelClass(LibraryParcel.class)
public class AndroidApplication extends Application{
//...
}
If that works, you could use the following code to wrap/unwrap:
Parcelable wrapped = Parcels.wrap(new Example("Andy", 42));
Example example = Parcels.unwrap(wrapped);
example.getName(); // Andy
example.getAge(); // 42
Did you try to use Bundle ?
For example if you have parcelable class User
Bundle bundle = new Bundle();
List<User> users = new ArrayList<User>();
String userName = user.setUserName("some name");
boolean isOnline = user.setOnline(true);
users.add(user);
bundle.putParcelableArrayList("USER", users);
And you may retrieve this as:
public ArrayList<User> getParcelableArrayList(Bundle bundle){
List<User> userList = new ArrayList<User>();
userList = bundle.getParcelableArrayList("USER");
}