package demo;
//import java.io.BufferedReader;
import java.util.Iterator;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import java.io.File; // Import the File class
//import java.io.FileNotFoundException; // Import this class to handle errors
//import java.util.Scanner; // Import the Scanner class to read text files
public class Book
{
public static void main(String[] args)
{
Client client = ClientBuilder.newClient();
WebTarget target = client.target("https://localhost:8090/webservice.php?opcion=get");
Invocation.Builder invoBuilder = target.request(MediaType.APPLICATION_JSON);
Response response = invoBuilder.get();
//int k = 0;
//String data = "";
try
{
if(Response.Status.OK.getStatusCode() == response.getStatus())
{
//k++;
String json = response.readEntity(String.class);
//jsonPlanet = jsonPlanet.replace("[", "").replace("]", "");
//json = json.replace("{\"" + String.valueOf(k) + "\":", "");
System.out.println(json);
/*try {
File myObj = new File("D:\\Resultado.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}*/
// JSONObject obj = new JSONObject(json);
JSONObject obj = new JSONObject(json);
//JSONObject songs= json.getJSONObject("songs");
Iterator x = obj.keys();
JSONArray jsonArray = new JSONArray();
int y = 0;
while (x.hasNext()){
String key = (String) x.next();
jsonArray.put(obj.get(key));
System.out.println(jsonArray.getJSONObject(y));
LeerCamposJSON(jsonArray.getJSONObject(y).toString());
y++;
}
/*for(int i=0; i<arr.length(); i++){
JSONObject o = arr.getJSONObject(i);
System.out.println(o);
}*/
}
/*else
{
System.out.println("Error contacting the web service! " + response.getStatus());
}*/
}
catch(JSONException e)
{
e.printStackTrace();
}
}
public static void LeerCamposJSON(String json)
{
try
{
JSONObject obj = new JSONObject(json);
int counter_id = obj.getInt("Counter_id");
String name = obj.getString("name");
String birthday = obj.getString("birthday");
String gender = obj.getString("gender");
String nationality = obj.getString("nationality");
String residencectry = obj.getString("residencectry");
String crraddress = obj.getString("crrAddress");
String occupation = obj.getString("occupation");
String maritalsts = obj.getString("maritalsts");
String homeph = obj.getString("homeph");
String movilph = obj.getString("movilph");
String email = obj.getString("email");
String idtype = obj.getString("idtype");
String idnumber = obj.getString("idnumber");
String idexp = obj.getString("idexp");
String crremployer = obj.getString("crremployer");
String workph = obj.getString("workph");
String companyaddr = obj.getString("companyaddr");
String companyemail = obj.getString("companyemail");
String website = obj.getString("website");
String mthincome = obj.getString("mthincome");
String procedenceincome = obj.getString("procedenceincome");
String actingas = obj.getString("actingas");
String servicetype = obj.getString("servicetype");
String moreinfo = obj.getString("moreinfo");
String wheredidufindus = obj.getString("wheredidufindus");
String created_at = obj.getString("created_at");
System.out.println("Counter ID: " + counter_id);
System.out.println("Name: " + name);
System.out.println("BirthDay: " + birthday);
System.out.println("Gender: " + gender);
System.out.println("Nationality: " + nationality);
System.out.println("residencectry: " + residencectry);
System.out.println("crraddress: " + crraddress);
System.out.println("occupation: " + occupation);
System.out.println("maritalsts: " + maritalsts);
System.out.println("homeph: " + homeph);
System.out.println("movilph: " + movilph);
System.out.println("email: " + email);
System.out.println("idtype: " + idtype);
System.out.println("idnumber: " + idnumber);
System.out.println("idexp: " + idexp);
System.out.println("crremployer: " + crremployer);
System.out.println("workph: " + workph);
System.out.println("companyaddr: " + companyaddr);
System.out.println("companyemail: " + companyemail);
System.out.println("website: " + website);
System.out.println("mthincome: " + mthincome);
System.out.println("procedenceincome: " + procedenceincome);
System.out.println("actingas: " + actingas);
System.out.println("servicetype: " + servicetype);
System.out.println("moreinfo: " + moreinfo);
System.out.println("wheredidufindus: " + wheredidufindus);
System.out.println("created_at: " + created_at);
}
catch(JSONException e)
{
e.printStackTrace();
}
}
}
No hay comentarios:
Publicar un comentario