JAVA SIMPLE JSON TEST SOURCE
javax.json-api-1.0.jar : https://drive.google.com/open?id=0B7voYRgqBNAZNTNzcGpTcklvajQ
jettison-1.1.jar: https://drive.google.com/open?id=0B7voYRgqBNAZMWRiOHViaVJXT1E
json-simple-1.1.1.jar: https://drive.google.com/file/d/0B7voYRgqBNAZNUs2OXV1R19rLXc/view?usp=sharing
package testtest;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObjectBuilder;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
public class SelchkTest {
public static void main(String[] args){
// key와 value로 구성되어있는 HashMap 생성.
JSONObject obj = new JSONObject();
String obj_str ="";
try {
obj_str = URLEncoder.encode(obj.toString(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int cnt =950492;
obj_str =obj.toString();
String url = "http://192.0.61.1:8080/api/jsonTest";
StringBuffer sbParam = new StringBuffer();
for(int i=0;i<1000;i++){
obj.put("SEQ",cnt++);
tran(url, obj);
}
System.out.println(obj_str);
tran(url, obj);
}
public static void tran(String url, JSONObject obj){
BufferedWriter bw = null;
InputStream is = null;
HttpURLConnection conn = null;
try {
URL tUrl = new URL(url);
conn = (HttpURLConnection) tUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(40*1000);
conn.setReadTimeout(40*1000);
conn.connect();
bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
bw.write(obj.toJSONString());
bw.flush();
bw.close();
is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "UTF-8");
JSONObject object = (JSONObject)JSONValue.parse(isr);
System.out.println(object);
/* Object로 받을 경우 */
JSONObject head = (JSONObject) object.get("head");
}catch(IOException ioe){
ioe.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
if(bw!=null) bw.close();
if(is!=null) is.close();
if(conn!=null) conn.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}