Criei uma String semelhante a que eu recebo, com os(), e acabo recendo o mesmo erro.
String resposta1 = "({\"Codigo\": 2,\"Descricao\": \"Sem dados no perÃodo\"})";
System.out.println(resposta1);
JSONObject jsonObj = new JSONObject(resposta1);
Erro:
Exception in thread “main” org.json.JSONException: A JSONObject text must begin with ‘{’ at 1 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONObject.(JSONObject.java:196)
at org.json.JSONObject.(JSONObject.java:320)
at smsbahia.principal.Start.main(Start.java:28)
Eu pensei ate que fosse os espaços em branco. Mas fiz isso mesmo acima anteriormente, copiei a resposta e passei para o JsonObject. Pode ser na classe que eu faço a requisição, pois ela que me retorna essa String.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Consumidor {
public String sendPost(String url, String json) throws MinhaException {
try {
HttpURLConnection request = (HttpURLConnection) new URL(url).openConnection();
try {
request.setDoOutput(true);
request.setDoInput(true);
request.setRequestProperty("Content-Type", "application/json");
request.setRequestMethod("POST");
request.connect();
try (OutputStream outputStream = request.getOutputStream()) {
outputStream.write(json.getBytes("UTF-8"));
}
//int response = request.getResponseCode();
System.out.println(request);
return readResponse(request);
} finally {
request.disconnect();
}
} catch (IOException ex) {
throw new MinhaException(ex);
}
}
public String readResponse(HttpURLConnection request) throws IOException {
ByteArrayOutputStream os;
try (InputStream is = request.getInputStream()) {
os = new ByteArrayOutputStream();
int b;
while ((b = is.read()) != -1) {
os.write(b);
}
}
return new String(os.toByteArray());
}
public static class MinhaException extends Exception {
private static final long serialVersionUID = 1L;
public MinhaException(Throwable cause) {
super(cause);
}
}
}
Realmente ela retorna a String perfeitamente, mas o estranho é que eu mando imprimir a String imprimi da mesma forma que eu coloco manualmente, só que manualmente funciona, e via passagem de parâmetro não.
Eu chamo essa classe acima, no seguindo trecho da minha classe principal:
String url = "URL";
GeradorJson gj = new GeradorJson();
String json = gj.gerandoJSon2();
Consumidor c = new Consumidor();
try {
String resposta = c.sendPost(url, json);
System.out.println(resposta);
JSONObject jsonObj = new JSONObject(resposta);
System.out.println(jsonObj);
} catch (MinhaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Exception in thread “main” org.json.JSONException: Missing value at 2 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONTokener.nextValue(JSONTokener.java:404)
at org.json.JSONObject.(JSONObject.java:207)
at org.json.JSONTokener.nextValue(JSONTokener.java:380)
at org.json.JSONObject.(JSONObject.java:207)
at org.json.JSONObject.(JSONObject.java:320)
at smsbahia.principal.Start.main(Start.java:29)
Eu já removi as aspas “extras”. E o erro mudou, agora é esse.
Exception in thread “main” org.json.JSONException: Missing value at 2 [character 2 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:451)
at org.json.JSONTokener.nextValue(JSONTokener.java:404)
at org.json.JSONObject.(JSONObject.java:207)
at org.json.JSONTokener.nextValue(JSONTokener.java:380)
at org.json.JSONObject.(JSONObject.java:207)
at org.json.JSONObject.(JSONObject.java:320)
at smsbahia.principal.Start.main(Start.java:29)