Answer the question
In order to leave comments, you need to log in
Problems with encoding when working with VKontakte API via Java and GSON?
Hello!
I wrote a code in Java to get the username of Vkontakte with a given uid.
String request = "https://api.vk.com/method/users.get?uids=" + 1 + "&fields=first_name,last_name&access_token=" + accessToken; //для Павла Дурова
try {
URL url = new URL(request);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
BufferedReader inReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
while ((inputLine = inReader.readLine()) != null) {
int beginIndex = inputLine.indexOf('[');
int endIndex = inputLine.indexOf(']');
inputLine = inputLine.substring(beginIndex + 1, endIndex);
VKPerson vkPerson = new Gson().fromJson(inputLine, VKPerson.class);
System.out.println(vkPerson);
}
inReader.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
public class VKPerson {
private Integer uid;
private String first_name;
private String last_name;
public Integer getUid () {return uid;}
public String getFirst_name () {return first_name;}
public String getLast_name () {return last_name;}
public void setUid (Integer uid) {this.uid = uid;}
public void setFirst_name (String first_name) {this.first_name = first_name;}
public void setLast_name (String last_name) {this.last_name = last_name;}
public String toString () {
//TODO: charset conversion
return String.format("uid: %d; first_name: %s; last_name: %s", uid, first_name, last_name);
}
}
Answer the question
In order to leave comments, you need to log in
most likely the problem is
BufferedReader inReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
set the required encoding for InputStreamReader
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question