I
I
ideas4ru2012-10-22 15:16:39
Java
ideas4ru, 2012-10-22 15:16:39

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);
    }

VKPerson class for GSON:
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);
  }
}

As a result, the response line looks like this
{"response":[{"uid":1,"first_name":"ПавеÐ"","last_name":"уров"}]}
and VKPerson prints
uid: one; first_name: ПавеÐ"; last_name: Дуров
I know that I need to convert data from cp-1251 utf-8, in which VK gives Russian names. But apparently this should already be done after the work of Gson().fromJson. But then, from each character of the original string, two bytes are obtained in the strings of the VKPerson class.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Moxa, 2012-10-22
@Moxa

most likely the problem is
BufferedReader inReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
set the required encoding for InputStreamReader

M
Moxa, 2012-10-22
@Moxa

Are you sure it's cp1251? something tells me that after all utf-8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question