2

我有一个链接“http://bkwsu.org/thoughtText?lang=hi”,我从中检索了 json 数据,从中提取了印地语中的“文本”,然后是 java。然后我尝试将这个印地语文本发布到 fb 页面。现在问题是当我将数据发布到 fb 页面时,它会显示一些乱码。这是我的代码:

import java.util.*;
import java.io.*;
import com.restfb.*;
import java.net.*;
import com.restfb.json.*;
import com.restfb.types.*;
public class MurliPost
{
    public static void main(String args[])throws Exception{
        String temp1="",temp = "";
        URL u=new URL("http://bkwsu.org/thoughtText?lang=hi");
        HttpURLConnection uc=(HttpURLConnection) u.openConnection();
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.29 (KHTML, like Gecko) Chrome/12.0.733.0 Safari/534.29");
        uc.setRequestProperty("Connection","keep-alive");
        uc.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml");
        uc.setRequestProperty("Accept-Language","en-US,en;q=0.8");
        uc.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        uc.setRequestMethod("GET");
        uc.setInstanceFollowRedirects(false);
        BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        while ((temp=br.readLine())!=null){temp1+="\n"+temp;}
        String murli=temp1.substring(temp1.indexOf("'text':'")+8,temp1.indexOf("',\n'topic':"));
        murli=murli.replace("\\r\\n\\r\\n","\n\n");
        murli=murli.replace("\\r\\n","\n\n");
        murli=murli.replaceAll("[ \t]+", " ");
        Date date=new Date();
        murli=String.valueOf(date.getDate())+"/"+String.valueOf(date.getMonth()+1)+"/"+String.valueOf(1990+date.getYear())+"\n\n"+murli;
        System.out.println(murli);
        DefaultFacebookClient facebookClient=new DefaultFacebookClient("your access token goes here");
        JsonObject j=facebookClient.fetchObject("me/accounts",JsonObject.class);
        JsonArray acc=j.getJsonArray("data");
        for(int i=0;i<acc.length();i++) 
            if(acc.getJsonObject(i).getString("name").equals("Brahma Kumaris")) {
                DefaultFacebookClient facebookClient1=new DefaultFacebookClient(acc.getJsonObject(i).getString("access_token"));
                String posturl=acc.getJsonObject(i).getString("id")+"/feed";
                FacebookType publishMessageResponse=facebookClient1.publish(posturl,FacebookType.class,Parameter.with("message",murli));
                System.out.println("success");
                break;
            }
    }
}

那么如何在java中处理这个问题呢?

4

1 回答 1

2

您应该在实例化 InputStreamReader 时指定编码,否则默认系统编码将应用于 GET 请求的输出,如果它与响应中返回的编码不匹配,则会损坏字符。

于 2012-05-23T09:25:42.510 回答