打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
java使用SAE中文分词服务

使用java调用SAE中文分词服务

采用post方式提交context值

分词后使用json返回分词数组

代码如下:
 public static String sendRequest(String context){
 
  BufferedReader in = null;
  StringBuffer result = new StringBuffer();
  HttpURLConnection httpConnection =null;
  try {

   //分词请求地址
   URL url = new URL("http://segment.sae.sina.com.cn/urlclient.php?encoding=UTF-8&word_tag=1");
   httpConnection = (HttpURLConnection) url.openConnection();
   httpConnection.setUseCaches(false);
   httpConnection.setDoOutput(true);
   httpConnection.setRequestMethod("POST");
   httpConnection.setDoOutput(true);
   httpConnection.setDoInput(true);
   httpConnection.setReadTimeout(10000);
   httpConnection.setConnectTimeout(10000);
    byte[] b = context.getBytes("utf-8");
    httpConnection.getOutputStream().write(b, 0, b.length);
    httpConnection.getOutputStream().flush();
    httpConnection.getOutputStream().close();
    in = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(),"utf-8"));
    while (true) {
               String line = in.readLine();
               if (line == null) {
                 break;
               }
               else {
                   result.append(line);
               }
             }
    System.out.println("******:"+result.toString());

   
  } catch (IOException  e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally{
            try {
                if(in!=null){
                    in.close();
                }
                if(httpConnection!=null){
                 httpConnection.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  return result.toString();

  
  
 }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
java对本地日志文件的读写
静态HTML生成模式代码
[入门阅读]怎样在android中解析JSON
移动资费提示过滤
Java 7中自动关闭流和catch多个异常
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服