I am creating an app that makes a GET Request to an API and receives a JSON and I need to create class that holds the information. This is what I have tried :
package apiwebapprequest;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class APIWebAppRequest {
public static void main(String[] args) throws IOException {
Gson gson = new Gson();
Object obj = new Object();
try {
URL url = new URL("xxx");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while((inputLine = in.readLine())!= null){
System.out.println(inputLine);
in.close();
gson.toJson(obj,inputLine);
}
}catch(Exception e) {System.out.println(e);}
}
}
This is the object class:
package apiwebapprequest;
public class Object {
private String cif;
private String data_creare;
public String getCif() {
return cif;
}
public void setCif(String cif) {
this.cif = cif;
}
public String getData_creare() {
return data_creare;
}
public void setData_creare(String data_creare) {
this.data_creare = data_creare;
}
}
On line 29 - > gson.toJson(obj,inputLine); it gives me an error. Can you please tell me how to do it ? I can`t find how to fix it or modify in other way that it works
The ideea is that i get out when i make request in inputLine I have json and I want to save the fields separate in Object properties