Reputation: 43
My problem is I can't or I don't know how to retrieve my JSON data in the class valeur
which is the class that contains the setter and getter of my variables.
I have a class which displays my variables which is AfficheurListView
.
I want just parsing my JSON and the result of this needs to be in the getter and setter of the class value to be displayed on the method onCreate
of my class AfficheurListView
.
I know how use it in the try/catch but I don't want to show my variable mpx in the try/catch
AfficheurListView.java
public class AfficheurListView extends AppCompatActivity {
String json_string2;
String json_string;
JSONObject jObj = null;
TextView textView;
String mpx,rds,al,ar,frequence,pilots,id,id_SIGFOX,timestamps,rf;
Valeurs valeurss=new Valeurs( mpx,rds,al,ar,frequence,pilots,id,id_SIGFOX,timestamps,rf);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_listview_layout);
json_string=getIntent().getExtras().getString("json_data");
json_string2=getIntent().getExtras().getString("json_Mpx");
textView=(TextView)findViewById(R.id.textView4);
textView.setText(valeurss.getMpx()); //display the variable mpx of the getter
try {
String mpx,rds,al,ar,frequence,pilots,id,id_SIGFOX,timestamps,rf;
jObj = new JSONObject(json_string);
mpx= jObj.getString("MPX");
rds =jObj.getString("RDS");
rf=jObj.getString("RF");
frequence =jObj.getString("Frequence");
timestamps=jObj.getString("timestamp");
id= jObj.getString("id");
id_SIGFOX= jObj.getString("id_SIGFOX");
pilots= jObj.getString("PILOT");
al= jObj.getString("a_l");
ar= jObj.getString("a_r");
Valeurs valeurs=new Valeurs(mpx,rds,al,ar,frequence,pilots,id,timestamps,id_SIGFOX,rf);
valeurss=valeurs;
} catch (JSONException e) {
e.printStackTrace();}
}
Valeurs.java
public class Valeurs{
private String mpx,rds,al,ar,pilots,frequence,id,timestamps,id_SIGFOX,rf;
public Valeurs(String mpx, String rds, String al, String ar, String pilots, String frequence, String id, String timestamps, String id_SIGFOX, String rf)
{
this.setMpx(mpx);
this.setRds(rds);
this.setAl(al);
this.setAr(ar);
this.setPilots(pilots);
this.setFrequence(frequence);
this.setId(id);
this.setTimestamps(timestamps);
this.setId_SIGFOX(id_SIGFOX);
this.setRf(rf);
}
public String getMpx() {
return mpx;
}
public void setMpx(String mpx) {
this.mpx = mpx;
}
public String getRds() {
return rds;
}
public void setRds(String rds) {
this.rds = rds;
}
public String getAl() {
return al;
}
public void setAl(String al) {
this.al = al;
}
public String getAr() {
return ar;
}
public void setAr(String ar) {
this.ar = ar;
}
public String getPilots() {
return pilots;
}
public void setPilots(String pilots) {
this.pilots = pilots;
}
public String getFrequence() {
return frequence;
}
public void setFrequence(String frequence) {
this.frequence = frequence;
}
public String getTimestamps() {
return timestamps;
}
public void setTimestamps(String timestamps) {
this.timestamps = timestamps;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getId_SIGFOX() {
return id_SIGFOX;
}
public void setId_SIGFOX(String id_SIGFOX) {
this.id_SIGFOX = id_SIGFOX;
}
public String getRf() {
return rf;
}
public void setRf(String rf) {
this.rf = rf;
}
}
Upvotes: 1
Views: 7468
Reputation: 711
First, I highly recommend you to use GSON or Jackson to convert String to your Java object (in this case your class Valeurs.java) and Java object to String, it's called serialization and deserialization.
Then, in try statement in AfficheurListView.java, edit to:
Gson gson = new Gson();
Valeurs valeurs = gson.fromJson(json_string, Valeurs.class);
Now, you can get mpx with:
valeurs.getMpx();
Note: Field name must to match with your JSON fields.
Note 2: Do not use plural for class name (example: Valeurs -> Valeur).
Hope it helps you!
EDIT: you need to implement something like that:
public class AfficheurListView extends AppCompatActivity {
String json_string2;
String json_string;
TextView textView;
String mpx, rds, al, ar, frequence, pilots, id, id_SIGFOX, timestamps, rf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_listview_layout);
json_string = getIntent().getExtras().getString("json_data");
json_string2 = getIntent().getExtras().getString("json_Mpx");
textView = (TextView) findViewById(R.id.textView4);
try {
// Json Object {}
/* Useless, because you use GSON instead of this
String mpx, rds, al, ar, frequence, pilots, id, id_SIGFOX, timestamps, rf;
jObj = new JSONObject(json_string);
mpx = jObj.getString("MPX");
rds = jObj.getString("RDS");
rf = jObj.getString("RF");
frequence = jObj.getString("Frequence");
timestamps = jObj.getString("timestamp");
id = jObj.getString("id");
id_SIGFOX = jObj.getString("id_SIGFOX");
pilots = jObj.getString("PILOT");
al = jObj.getString("a_l");
ar = jObj.getString("a_r");
Gson gson = new Gson();
Valeurs valeurs = new Valeurs(mpx, rds, al, ar, frequence, pilots, id, timestamps, id_SIGFOX, rf);*/
// So, now 'valeurs' and 'valeurs2' are
Valeurs valeurs = gson.fromJson(json_string, Valeurs.class);
Valeurs valeurs2 = gson.fromJson(json_string2, Valeurs.class);
// if your program is here, then you have no parsing error, so set your textView
textView.setText(valeurs2.getMpx());
} catch (Exception e) {
// You have an error
textView.setText(e.getMessage());
}
}
}
Upvotes: 1