最近学到的java常识

1.请求服务器的接口:其中涉及到http请求,url。

依赖org.apache.httpcomponents/httpclient/4.3.4

//1. 创建默认的httpClient实例.

CloseableHttpClient httpclient =null;

httpclient = HttpClients.createDefault();

//2. 创建httppost

HttpPost httpPost =newHttpPost(url1);

httpPost.addHeader(HTTP.CONTENT_TYPE,"application/x-www-form-urlencoded");

// 3. 参数

String exp_id="0^0^0^0^0^0^0^0";

String utf16_gpoint="12950074.7,4836976.15";

List postBody =newArrayList();

postBody.add(newBasicNameValuePair("queryString",query));

postBody.add(newBasicNameValuePair("forceQuery","1"));

postBody.add(newBasicNameValuePair("exp_id","0^0^0^0^0^0^0^0"));

postBody.add(newBasicNameValuePair("gpoint","12950074.7,4836976.15"));

StringBuilder postData =newStringBuilder();

for(BasicNameValuePair namevalue : postBody) {

postData.append(namevalue.getName()).append("=")

.append(StringHelper.urlEncodeUtf16le(namevalue.getValue())).append("&");

}

postData.deleteCharAt(postData.length() -1);

StringEntity se =newStringEntity(postData.toString());

se.setContentEncoding("UTF-16LE");

se.setContentType("application/json");

httpPost.setEntity(se);

response = httpclient.execute(httpPost);

//解析返结果

HttpEntity entity = response.getEntity();

2. hadoop java map-reduce

依赖org.apache.hadoop  hadoop-core  0.20.2

程序输入处,int status = ToolRunner.run(new Main(),args);

构建Main类,extends Configured implements Tool 。在Main类中,重写run方法。在run方法中,定义Configuration conf = getConf(); 并进行配置。定义Jobjob =newJob(conf);可配置输入输出路径,配置mapreduce等各种。在Main类中实现public static class InputMapper extends Mapper,public static class OutputReducer extends Reducer

3. xml 处理

StringReader reader =newStringReader(resStr);

InputSource source =newInputSource(reader);

SAXReader sr =newSAXReader();

Document doc = sr.read(source);

Element root = doc.getRootElement();

//遍历根节点下的子节点(同样也可以遍历某个含子节点的子节点)

for(Object obj : root.elements()) {

Element ele = (Element) obj;

//获取子节点的属性

if(ele.getName() =="process_node"){

for(Object ob:ele.elements()){

4. json串生成

JSONObject jsonObject =newJSONObject();

jsonObject.put("weight",((Element) xx).attribute("weight").getValue());

String jsonStr == jsonObject.toString();

5. maven 打包的配置    

http://www.jianshu.com/p/7a0e20b30401  

https://maven.apache.org/plugins/maven-shade-plugin/

include/exclude 掉

junit/framework/**     org/junit/experimental/**

通过设置 MainClass 创建一个可执行 Jar 包

包括把依赖打一个jar包、依赖jar包的冲突解决  relocation 

6. 多线程

你可能感兴趣的:(最近学到的java常识)