My problem is the following, I bring a json
from my api
, the same I pass it to a JSONArray
the topic this is that I want to arm a Array<Especialidad>
and I do not know how to do it.
Specialty_connect
public class Especialidad_connect extends AsyncTask<String, Void, String> {
public String url = "http://192.168.1.55:8080/especialidad/";
public JSONArray jArray;
@Override
protected String doInBackground(String... strings) {
try {
getJSON(url);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public JSONArray getJSON(String url) throws IOException, JSONException {
InputStream is = null;
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
jArray = new JSONArray(result);
return jArray;
}
I want to take this
To this
Where the json
that I bring from my api
is [{"idEspecialidad":"1","nombre":"clinico"},{"idEspecialidad":"2","nombre":"pediatra"}]
Specialty
public class Especialidad {
long id;
String nombre;
public Especialidad(long idEspecialidad, String nombre) {
this.id = idEspecialidad;
this.nombre = nombre;
}
public Especialidad() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}