JAVA Garden (BY REDTROY TEAM)

Working experience

Google Util

package com.qiaoju.app.qiaoju.grab;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import net.sf.json.JSONObject;

public class GoogleUtil {

public static String grabScenery(String address) {
String google = “https://maps.google.com/maps?f=q&source=s_q&output=js&hl=zh-CN&geocode=&abauth=51d29ab3wiyAakz6PIQ6F0TPy1CE2agSnHo&authuser=0&q=”
+ URLEncoder.encode(“景点 loc:” + address);
String script = grab(google);
String json = script.substring(
script.indexOf(“markers:”) + “markers:”.length(),
script.indexOf(“layers:”) – 1);

net.sf.json.JSONObject fromObject = net.sf.json.JSONObject
.fromObject(“{markers:” + json + “}”);
net.sf.json.JSONArray jsonArray = fromObject.getJSONArray(“markers”);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < jsonArray.size(); i++) {
if (i 0) {
bos.write(bytes, 0, len);
}

bis.close();
bos.close();
return new String(bos.toByteArray(), “UTF-8″);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void main(String[] args) {
String coordination = GoogleUtil
.grabCoordination(“48 Ranna Dr, Sydney, NS, B1P3Y7″);
System.out.println(coordination);
}
}

Posted by redtroy at 12:46pm | 未分类 | no comments

Youdao Translator

package com.qiaoju.app.qiaoju.grab;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class YoudaoTranslator {

private static final String query = “http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null”;

public static final String translate(String content) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(query);
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair(“type”, “AUTO”));
nvps.add(new BasicNameValuePair(“i”, content));
nvps.add(new BasicNameValuePair(“doctype”, “json”));
nvps.add(new BasicNameValuePair(“keyfrom”, “fanyi.web”));
nvps.add(new BasicNameValuePair(“ue”, “UTF-8″));
nvps.add(new BasicNameValuePair(“typoResult”, “true”));
post.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();
// EntityUtils.consume(entity);
InputStream inputstream = entity.getContent();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = 0;
byte[] buff = new byte[1024];
while ((read = inputstream.read(buff)) > 0) {
bos.write(buff, 0, read);
}

String json = new String(bos.toByteArray(), “UTF-8″);

JSONObject fromObject = JSONObject.fromObject(json);
net.sf.json.JSONArray jsonArray = fromObject
.getJSONArray(“translateResult”);
String array = jsonArray.toString();
jsonArray = JSONArray.fromObject(array.substring(1,
array.length() – 1));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < jsonArray.size(); i++) {
String scenior = jsonArray.getJSONObject(i).getString("tgt");
sb.append(scenior);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}

public static void main(String args[]) {
System.out.println(YoudaoTranslator
.translate("I'm troy,sdfjalsfjsjafdfa;sdf"));
}
}

Posted by redtroy at 12:45pm | 未分类 | no comments

关于shutdownhook

package com.goldensky.dbtool;

import static org.junit.Assert.*;

import org.junit.Test;

public class ShutDownHookTest {

@Test
public void test() {
Runtime.getRuntime().addShutdownHook(new Thread(new ShutDownHook()));
}

private class ShutDownHook implements Runnable{

public void run() {
// TODO Auto-generated method stub
System.out.println(“system shutting down”);
}

}

}

Posted by redtroy at 09:43pm | 未分类 | no comments

XSD-简单元素

XSD-简单元素

Posted by redtroy at 04:34pm | 未分类 | no comments

DTD简介

DTD简介

Posted by redtroy at 11:04am | 未分类 | no comments

Eclipse TPTP 安装配置手记

TPTP

Posted by redtroy at 02:23pm | 未分类 | no comments

WebPageSnap

WebPageSnap

Posted by redtroy at 10:55am | 未分类 | no comments

ubuntu oracle client installation

runInstaller -ignoreSysPreReqs

Posted by redtroy at 02:31pm | 未分类 | no comments

BufferdImage 生理曲线

BufferdImage 生理曲线

Posted by redtroy at 12:51pm | 未分类 | no comments

fcitx配置

fcitx 配置

Posted by redtroy at 12:47pm | About Linux | no comments

下一页 »