어바웃 쪽에 광고 연동 프로그램을 만드는 중...
solr의 데이터를 json으로 받아서 java로 parsing 하는 예제임..
라이브러리는 Apache Commons 와 log4J 1.2(!!! 반드시 이걸 사용해야 함..난 최신버전이 좋아요~--> 개고생으로 이어질꺼임!)
package com.lifebook.util.about;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class retrieveGoods {
private static Log log = LogFactory.getLog(retrieveGoods.class);
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//String strClassPath = System.getProperty("java.class.path");
log.info("Start Process...");
//log.info("java.class.path..." + strClassPath);
String bookInfo = "";
try {
String urlStr = "";
urlStr = "http://IP:PORT/solr/CORE명/select?q=검색어~~";
URL url = new URL(urlStr);
log.info("Call URL : " + url.toString());
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream(), "UTF-8"));
String inputLine;
while ((inputLine = in.readLine()) != null)
bookInfo += inputLine;
in.close();
JSONObject json = JSONObject.fromObject(bookInfo);
JSONObject response = JSONObject.fromObject(json.getString("response"));
JSONArray docs = response.getJSONArray("docs");
System.out.println(docs.size());
JSONObject detailInfo = null;
for(int i = 0 ; i < docs.size(); i ++){
detailInfo = (JSONObject) docs.get(i);
System.out.println(detailInfo);
System.out.println(detailInfo.get("prma_name"));
}
log.info("End Process...");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("error..." + e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("error..." + e);
}
}
}
|
댓글 없음:
댓글 쓰기