Дано: создаю бота для Telegram. Отправляю запрос через http, получаю ответ в виде кучи текста. Хочу вытащить нужное: имя пользователя и текст.
Вопрос: в чем ошибка? Как я понимаю, сейчас программа вроде_как_должна_вытаскивать "username" и "text"
Код:
Код:
package com.TelegramBot;
import org.json.JSONArray;
import org.json.JSONObject;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
public class TelegramBot {
public String URL = "https://api.telegram.org/bot1<TokenMyBot>/";
public void getMe() {
try {
StringBuilder URL_getMe = new StringBuilder();
URL_getMe.append(URL).append("getMe");
URL url = null;
url = new URL(URL_getMe.toString());
url.openConnection();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
print_content(con);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getUpdates() {
try {
StringBuilder URL_getMe = new StringBuilder();
URL_getMe.append(URL).append("getUpdates");
URL url = new URL(URL_getMe.toString());
url.openConnection();
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
print_content(con);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void print_content(HttpsURLConnection con) {
if (con != null) {
try {
System.out.println("****** Content of the URL ********");
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
while ((input = br.readLine()) != null) {
System.out.println(input);
JSONObject rootObj = new JSONObject(input);
JSONObject obj = rootObj
.getJSONObject("username")
.getJSONObject("text");
System.out.println(rootObj);
}
System.out.println();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
TelegramBot main = new TelegramBot();
//main.getMe();
main.getUpdates();
}
}
Вывод если без объектов json:
Код:
****** Content of the URL ********
{"ok":true,"result":[{"update_id":123804251,
"message":{"message_id":44,"from":{"id":186320203,"first_name":"Alexander","last_name":"Ivanov","username":"Nevender"},"chat":{"id":186320203,"first_name":"Alexander","last_name":"Ivanov","username":"Nevender","type":"private"},"date":1461865375,"text":"iriskin is a nice man"}}]}
Вывод с объектами json:
Код:
****** Content of the URL ********
{"ok":true,"result":[{"update_id":123804251,
Exception in thread "main" org.json.JSONException: A JSONObject text must end with '}' at 45 [character 46 line 1]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONObject.<init>(JSONObject.java:194)
at org.json.JSONTokener.nextValue(JSONTokener.java:364)
at org.json.JSONArray.<init>(JSONArray.java:114)
at org.json.JSONTokener.nextValue(JSONTokener.java:367)
at org.json.JSONObject.<init>(JSONObject.java:208)
at org.json.JSONObject.<init>(JSONObject.java:312)
at com.TelegramBot.TelegramBot.print_content(TelegramBot.java:62)
at com.TelegramBot.TelegramBot.getUpdates(TelegramBot.java:43)
at com.TelegramBot.TelegramBot.main(TelegramBot.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code 1