I want to add post.java between DataRetriver.java code .Because i want to use pojo object and post.java has pojo object. I think I can add id = array_items.optInt("id"); but i know it.
DataRetriver.java
for (int i = 0; i < names_count; i++) {
JSONObject array_items = jsonArray.getJSONObject(i);
ListValues jsonValues, pictureValue;
switch (type) {
case 1:
id = array_items.optInt("id");
name = array_items.optString("name");
jsonValues = new ListValues(id, name);
listValues.add(jsonValues);
break;
case 2:
id = array_items.optInt("userId");
name = array_items.optString("title");
if (id == recieved_id) {
jsonValues = new ListValues(id, name);
listValues.add(jsonValues);
}
break;
Post.java
public class Post implements Serializable {
#SerializedName("userId")
private int userId;
#SerializedName("name")
#Expose
public String name;
#SerializedName("id")
private int id;
#SerializedName("title")
private String title;
#SerializedName("body")
private String body;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
Your advice important for me
Thank you!
Related
I know there are a few questions on stackoverflow regarding this problem. But I have have been spending hours trying to resolve this error without any success.
I am using the mysql database to store the values.
I keep on getting the error message from the
com.example.springboot.Recipe file.
This is springboot recipe file
package com.example.springboot;
import com.example.springboot.Recipe;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
#Entity // This tells Hibernate to make a table out of this class
public class Recipe {
public Recipe(){
}
public Recipe(Integer id, String name, String description, String type, Integer preptime, Integer cooktime, String content, Integer difficulty){
this.id = id;
this.name = name;
this.description = description;
this.type = type;
this.preptime = preptimee;
this.cooktime = cooktime;
this.content = content;
this.difficulty = difficulty;
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
private String type;
private Integer preptime;
private Integer cooktime;
#Column(columnDefinition = "TEXT")
private String content;
private Integer difficulty;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return name;
}
public void setTitle(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getDifficulty() {
return difficulty;
}
public void setDifficulty(Integer difficulty) {
this.difficulty = difficulty;
}
public Integer getCookingtime() {
return cooktime;
}
public void setCookingtimeime(Integer cooktime) {
this.cooktime = cooktime;
}
public Integer getPreparationtime() {
return preptime;
}
public void setPreparationtime(Integer preptime) {
this.preptime = preptime;
}
}
Main Controller:
#PutMapping("/recipes/edit/{id}")
void updateRecipe2(#PathVariable int id, #RequestBody Recipe recipe ) {
Recipe recipe_ = recipeRepository.findById(id).get();
recipe_.setTitle(recipe.getTitle());
System.out.println("sss " + recipe.getname());
System.out.println("change");
recipeRepository.save(recipe_);
}
service.ts:
updateRecipe2 (id: number, recipe: any): Observable<any > {
const url = `${this.usersUrl}/edit/${id}`;
return this.http.put(url ,recipe);
}
where the updateRecipe2 gets called:
save(): void {
const id = +this.route.snapshot.paramMap.get('name');
this.recipeService.updateRecipe2(id, this.recipes)
.subscribe(() => this.gotoUserList());
}
as soon as the user clicks save this functions saves the changes made.
I hope the code snippets that I provided are enough to help solve the problem.
Thank you in advance.
I am building a rest api with spring boot and I am using angularjs as it's frontend. I am pretty new to web-development.
You are sending a list of recipes to an api endpoint that expects a single recipe object.
Your options are:
Send only one recipe object at a time, for example:
this.recipeService.updateRecipe2(id, this.recipes[0])
OR: create a new API endpoint to accept a list of recipes, to edit them in "batch"
#PutMapping("/recipes/edit")
void updateRecipes(#RequestBody List<Recipe> recipe ) {
my Example:
Use:
#PostMapping
Code:
public void setTransacciones(List<Transacciones> transacciones) {
this.transacciones = transacciones;
}
CodeBean:
public class Transacciones {
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
private String text;
}
Post(raw):
{
"transacciones" : [ {"text" : "1"}, {"text" : "2"} ]
}
Result:
{
"transacciones": [
{
"transaccionId": 2,
"text": "1"
},
{
"transaccionId": 3,
"text": "2"
}
]
}
BINGO!!
professionals!
I have a question. What is a better idea to connect 2 tables in spring boot via ID? As an example: we have 2 tables client and books. Each client can borrow a book and the status of book will be changed.
I know how to make it in DB using SQL. But the question is, how to do this in jpa/hibernate.
I have an error of ManyToMany or OneToMany.....
#Entity
public class Book implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#TableGenerator(name = "BOOK_GEN", allocationSize = 1)
#Id
#GeneratedValue(generator = "BOOK_GEN")
private int id;
private String book_name;
private String ISBN;
private String publish_year;
private String publisher;
private Boolean status;
#OneToMany(mappedBy="book" ,cascade=CascadeType.ALL , fetch = FetchType.LAZY)
private Collection<Client> authors =new ArrayList<Client>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public String getPublish_year() {
return publish_year;
}
public void setPublish_year(String publish_year) {
this.publish_year = publish_year;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public Collection<Author> getAuthors() {
return authors;
}
public void setClients(Collection<Client> clients) {
this.clients = clients;
}}
#Entity
public class Client implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#TableGenerator(name = "CLT_GEN", allocationSize = 1)
#Id
#GeneratedValue(generator = "CLT_GEN")
private int id;
private Boolean bookedstatus;
private Boolean bookstatus;
#ManyToOne(fetch = FetchType.LAZY)
private Book book;
private String name ;
private String surname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Boolean getBookedstatus() {
return bookedstatus;
}
public void setBookedstatus(Boolean bookedstatus) {
this.bookedstatus = bookedstatus;
}
public Boolean getBookstatus() {
return bookstatus;
}
public void setBookstatus(Boolean bookstatus) {
this.bookstatus = bookstatus;
}
}
Since you have mentioned "A client can borrow a book", one-to-one mapping should work the best for you. Please refer to the below code. Also you didn't mention the error you are facing with your current implementation.
#Entity
public class Book implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#TableGenerator(name = "BOOK_GEN", allocationSize = 1)
#Id
#GeneratedValue(generator = "BOOK_GEN")
private int id;
private String book_name;
private String ISBN;
private String publish_year;
private String publisher;
private Boolean status;
#OneToOne(fetch = FetchType.LAZY, mappedBy = "book", cascade = CascadeType.ALL)
private Collection<Client> authors;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBook_name() {
return book_name;
}
public void setBook_name(String book_name) {
this.book_name = book_name;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public String getPublish_year() {
return publish_year;
}
public void setPublish_year(String publish_year) {
this.publish_year = publish_year;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public Collection<Client> getAuthors() {
return authors;
}
public void setAuthors(Collection<Client> authors) {
this.authors = authors;
}
}
#Entity
public class Client implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#TableGenerator(name = "CLT_GEN", allocationSize = 1)
#Id
#GeneratedValue(generator = "CLT_GEN")
private int id;
private Boolean bookedstatus;
private Boolean bookstatus;
#OneToOne(fetch = FetchType.LAZY)
#PrimaryKeyJoinColumn
private Book book;
private String name ;
private String surname;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public Boolean getBookedstatus() {
return bookedstatus;
}
public void setBookedstatus(Boolean bookedstatus) {
this.bookedstatus = bookedstatus;
}
public Boolean getBookstatus() {
return bookstatus;
}
public void setBookstatus(Boolean bookstatus) {
this.bookstatus = bookstatus;
}
}
I'm newbie about android programming and Today I want to add data to another model class
and in another class model is same model
but I have no idea to add it
example
my first model
public class TelephoneModel {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and my second model
public class TelephoneDetailModel {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
and I have data in
List<TelephoneModel.DataBean> databeans = new ArrayList<>;
List<TelephoneDetailModel.DataBean> dataDetailbeans = new ArrayList<>;
and I want to add data databeans to dataDetailbeans
How to add it ,please give example for me,
thanks!
Hello change your model class like this (it is so simple to use)
--> TelephoneModel
public class TelephoneModel {
private String id;
private String name;
public TelephoneModel(String id,String name) {
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--> TelephoneDetailModel
public class TelephoneDetailModel {
public TelephoneDetailModel(String id, String name) {
this.id = id;
this.name = name;
}
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--> your activity
ArrayList<TelephoneModel> databeans = new ArrayList<>();
ArrayList<TelephoneDetailModel> dataDetailBeans = new ArrayList<>();
// add data into databean
databeans.add(new TelephoneModel("id-1", "name-1"));
databeans.add(new TelephoneModel("id-2", "name-2"));
// now add data databeans to dataDetailbeans
for (int i = 0; i < databeans.size(); i++) {
dataDetailBeans.add(new TelephoneDetailModel(databeans.get(i)
.getId(), databeans.get(i).getName()));
}
if any question then free to ask me...
Try this
List<TelephoneDetailModel.DataBean> databeans = new ArrayList();
TelephoneDetailModel.DataBean dataBean = new TelephoneDetailModel.DataBean();
dataBean.setId("1");
dataBean.setName("PREM");
databeans.add(dataBean);
TelephoneDetailModel.DataBean model1 = new TelephoneDetailModel.DataBean();
model1.setId("2");
model1.setName("PREM2");
databeans.add(model1);
TelephoneDetailModel.DataBean model2 = new TelephoneDetailModel.DataBean();
model2.setId("3");
model2.setName("PREM3");
databeans.add(model2);
TelephoneDetailModel.DataBean model3 = new TelephoneDetailModel.DataBean();
model3.setId("4");
model3.setName("PREM4");
databeans.add(model3);
for (int i = 0; i < databeans.size(); i++) {
Log.e("ID", databeans.get(i).getId());
Log.e("NAME", databeans.get(i).getName());
}
Out put
com.example.user33.demoapplication E/ID: 1
com.example.user33.demoapplication E/NAME: PREM
com.example.user33.demoapplication E/ID: 2
com.example.user33.demoapplication E/NAME: PREM2
com.example.user33.demoapplication E/ID: 3
com.example.user33.demoapplication E/NAME: PREM3
com.example.user33.demoapplication E/ID: 4
com.example.user33.demoapplication E/NAME: PREM4
Try this
public class TelephoneDetailModel {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String name;
public DataBean() {
}
public DataBean(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
now to add data in your arraylist try this
List<TelephoneDetailModel.DataBean> databeans = new ArrayList();
databeans.add(new TelephoneDetailModel.DataBean("6","NEW NAME"));
DataBean in TelephoneModel class and TelephoneDetailModel is different, you can't add the Datadean to another list
I'm creating the grid view with array list of category and un categorized product model class. Now I want to sort the list by date or name. See below my code.
Here this is my adapter.
public class CommonAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater inflator = null;
private List<Object> list;
public CommonAdapter(Context mContext, List<Object> list) {
super();
this.mContext = mContext;
this.list = list;
inflator = LayoutInflater.from(mContext);
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int position) {
return list.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflator.inflate(R.layout.row_categories, null);
holder = new ViewHolder();
holder.layout_bg = (RelativeLayout) convertView.findViewById(R.id.grid_bg);
holder.titleTextView = (TextView) convertView.findViewById(R.id.grid_item_title);
holder.txt_price = (TextView) convertView.findViewById(R.id.txt_price);
holder.img_notifier = (ImageView) convertView.findViewById(R.id.img_notifier);
holder.titleTextView.setTextColor(Color.WHITE);
holder.titleTextView.setTextSize(27);
holder.titleTextView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
holder.titleTextView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (list.get(position) instanceof Product) {
holder.titleTextView.setText(((Product) list.get(position)).getShortCode());
holder.img_notifier.setVisibility(ImageView.GONE);
holder.txt_price.setVisibility(TextView.VISIBLE);
NumberFormat format = NumberFormat.getCurrencyInstance();
double amount = Double.parseDouble(((Product) list.get(position)).getPrice()toString());
String formatAmount = NumberFormat.getCurrencyInstance().format(amount / 100);
holder.txt_price.setText(formatAmount);
}
if (list.get(position) instanceof Category) {
holder.titleTextView.setText(((CategoryWithProduct) list.get(position)).getShortCode());
holder.img_notifier.setVisibility(ImageView.VISIBLE);
holder.txt_price.setVisibility(TextView.GONE);
if (((Category) list.get(position)).getColor() != null) {
holder.layout_bg.setBackgroundColor(Color.parseColor(((Category) list.get(position)).getColor()));
} else {
}
}
return convertView;
}
static class ViewHolder {
RelativeLayout layout_bg;
TextView titleTextView, txt_price;
ImageView img_notifier;
}
This is product model classes
public class Product {
String id;
String name;
String price;
String createAt;
public Product(String id, String name, String price, String createAt) {
this.id = id;
this.name = name;
this.price = price;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
This is Category Model
public class Category {
String id;
String name;
String createAt;
public Category(String id, String name, String createAt) {
this.id = id;
this.name = name;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
In MainActivity.java
CommonAdapter commonAdapter = new CommonAdapter(getActivity(), commonArrayList);
grid_common.setAdapter(commonAdapter);
Here I tried with comparator, it's comes with object only!
Collections.sort(commonArrayList, new Comparator<Object>() {
#Override
public int compare(Object o1, Object o2) {
return 0;
}
});
See here both models have createAt and name fields, So I want to sort by createAt or by name in this ArrayList.
Create another object model class and add all method and variable there is in two separate class...
and set data manually then... using for loop and any other ..that suitable for you...
and you this third created object model for sorting your data...
Edited
Eg:
first class
class first{
String f_name,l_name;
public String getF_name() {
return f_name;
}
public void setF_name(String f_name) {
this.f_name = f_name;
}
public String getL_name() {
return l_name;
}
public void setL_name(String l_name) {
this.l_name = l_name;
}
}
Second class
public class second {
String f_name,l_name,m_name;
public String getF_name() {
return f_name;
}
public void setF_name(String f_name) {
this.f_name = f_name;
}
public String getL_name() {
return l_name;
}
public void setL_name(String l_name) {
this.l_name = l_name;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
}
third class
public class third{
String f_name,l_name,m_name;
public String getF_name() {
return f_name;
}
public void setF_name(String f_name) {
this.f_name = f_name;
}
public String getL_name() {
return l_name;
}
public void setL_name(String l_name) {
this.l_name = l_name;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
}
set all value of first and second into third...
and use third class for setup data and sorting data
Here is my advice:
public class Category {
String id;
String name;
String createAt;
...
}
public class Product extends Category{
String price;
....
}
Collections.sort(commonArrayList, new Comparator<Category>() {
#Override
public int compare(Category o1, Category o2) {
if(o1.getCreateAt()>o2.getCreateAt()){
return 1;
}else{
...
}
return 0;
}
});
Create an abstract class, put fields common in Product and Category and compare that class.
public abstract class BaseClass {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Your Public class:
public class Product extends BaseClass {
...
public Product(String id, String name, String price, String createAt) {
setId(id);
setName(name);
this.price = price;
this.createAt = createAt;
}
}
Category class:
public class Category extends BaseClass {
...
public Category(String id, String name, String createAt) {
setId(id);
setName(name);
this.createAt = createAt;
}
}
And compare like this:
Collections.sort("ArrayList<BaseClass>()", new Comparator<BaseClass>() {
#Override
public int compare(BaseClass baseClass, BaseClass t1) {
return baseClass.getName().compareTo(t1.getName());
}
});
If you wanna sort by date put date field to BaseClass.
Thanks for your advice. I found the answer. I just make class casting on the object inside the comparator.
See the code below,
Collections.sort(commonArrayList, new Comparator<Object>() {
#Override
public int compare(Object o1, Object o2) {
int res = 0;
if (o1 instanceof Category && o2 instanceof Category) {
res = (((Category) o1).getName().compareTo(((Category) o2).getName()));
} else if (o1 instanceof Product && o2 instanceof Product) {
res = (((Product) o1).getName().compareTo(((Product) o2).getName()));
} else if (o1 instanceof Category && o2 instanceof Product) {
res = (((Category) o1).getName().compareTo(((Product) o2).getName()));
} else if (o1 instanceof Product && o2 instanceof Category) {
res = (((Product) o1).getName().compareTo(((Category) o2).getName()));
}
return res;
}
});
If you have any simplified ideas, kindly post here..
Hope this sample solution in java may help :
Create an interface let say Data as follows
public interface Data {
}
Create the model classes as follows :
Product
public class Product implements Data{
String id;
String name;
String price;
String createAt;
public Product(String id, String name, String price, String createAt) {
this.id = id;
this.name = name;
this.price = price;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
Category
public class Category implements Data{
String id;
String name;
String createAt;
public Category(String id, String name, String createAt) {
this.id = id;
this.name = name;
this.createAt = createAt;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreateAt() {
return createAt;
}
public void setCreateAt(String createAt) {
this.createAt = createAt;
}
}
Now in main class of the project
public class TestSorting {
public static void main(String args[]) {
ArrayList<Data> categories = new ArrayList<>();
ArrayList<Data> products = new ArrayList<Data>();
// For Product
for (int i = 0; i < 10; i++) {
Product product = new Product("Prod" + i, "Product " + i, "" + i, System.currentTimeMillis() + "");
products.add(product);
}
// For category
for (int i = 10; i >=0; i--) {
Category category = new Category("Cat" + i, "Category " + i, System.currentTimeMillis() + "");
categories.add(category);
}
Collections.sort(categories, new Comparator<Data>() {
#Override
public int compare(Data data, Data data2) {
if(data instanceof Category)
{
int result=(((Category) data).getId().compareTo((((Category) data2).getId())));
return result;
}else if(data instanceof Product)
{
int result= (((Product) data).getId().compareTo(((Product) data2).getId()));
return result;
}else {
return 0;
}
}
});
System.out.println("******PRODUCT****************");
// For Product
for (int i = 0; i < products.size(); i++) {
Product product=((Product)products.get(i));
System.out.println(product.id+ " "+product.name);
}
System.out.println("\n\n"+"******Caterogy****************");
// For category
for (int i = 0; i < categories.size(); i++) {
Category category=((Category)categories.get(i));
System.out.println(category.id+ " "+category.name);
}
}
}
I am trying to get some the array of actors from Jira. The code for the wrapper is used in a Gson.fromJson call. I had used something similar with a json string that did not have an array in it that had the information I needed and it worked fine, so the issue seems to do with the array, but I am not 100% sure:
import com.google.gson.annotations.SerializedName;
public class JiraRoleJsonWrapper {
#SerializedName("self")
private String self;
#SerializedName("name")
private String name;
#SerializedName("id")
private int id;
#SerializedName("description")
private String description;
#SerializedName("actors")
private JiraActors[] actors;
public JiraActors[] getActors() {
return actors;
}
public void setActors(JiraActors[] actors) {
this.actors = actors;
}
public String getSelf() {
return self;
}
public void setSelf(String self) {
this.self = self;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String key) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*
public String[] getAvatarUrls() {
return avatarUrls;
}
public void setAvatarUrls(String[] avatarUrls) {
this.avatarUrls = avatarUrls;
}
*/
}
class JiraActors {
#SerializedName("id")
private int id;
#SerializedName("displayNme")
private String displayName;
#SerializedName("type")
private String type;
#SerializedName("name")
private String name;
//#SerializedName("avatarUrl")
//private String avatarUrl;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The json it would receive:
{
"self":"http://someserver.com:8080/apps/jira/rest/api/2/project/10741/role/10002",
"name":"Administrators",
"id":10002,
"description":"A project role",
"actors":[
{
"id":12432,
"displayName":"Joe Smith",
"type":"atlassian-user-role-actor",
"name":"joesmi",
"avatarUrl":"/apps/jira/secure/useravatar?size=xsmall&ownerId=dawsmi&avatarId=12245"
},
{
"id":12612,
"displayName":"Smurfette Desdemona",
"type":"atlassian-user-role-actor",
"name":"smudes",
"avatarUrl":"/apps/jira/secure/useravatar?size=xsmall&ownerId=lamade&avatarId=10100"
},
This shows two actors and the format of the json. Please note I did not put a complete json response. It just shows two actors.
In my code, I tried the following to retrieve the actors:
InputStream is = response.getEntityInputStream();
Reader reader = new InputStreamReader(is);
Gson gson = new Gson();
JiraRoleJsonWrapper[] jiraRoleJsonWrapper = gson.fromJson(reader, JiraRoleJsonWrapper[].class);
for (JiraRoleJsonWrapper w : jiraRoleJsonWrapper) {
JiraActors[] a = w.getActors();
String name = a.getName();
It does not find getName for some reason. I am not sure why.
I figured it out.
I change the setActors to
public void setActors(ArrayList<JiraActors> actors) {
this.actors = actors;
}
Then I was able to get the array list and get access to the getName() method of JiraActors.