서버쪽은 tomcat6.0으로 test 용 서블릿을 이용하여 구현하였습니다.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String data = null;
InputStream is = request.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
while((len = is.read(buf))>0){
baos.write(buf, 0, len);
}
data = new String(baos.toByteArray(), "UTF-8");
System.out.println(data);
Connection connect=null;
Statement st=null;
String url = "jdbc:sqlserver://59.10.61.199:1433;
String userid = "sa";
String passwd = ""
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connect = DriverManager.getConnection(url,userid,passwd);
st = connect.createStatement();
JSONArray jsonarr = new JSONArray(data);
for(int i=0;i<jsonarr.length();i++){
JSONObject jsonobj = jsonarr.getJSONObject(i);
System.out.println(jsonobj.getString("User_ID"));
System.out.println(jsonobj.getString("Description"));
String sql = "DECLARE " +
"@return_value int EXEC " +
"@return_value = [dbo].[PDA_BreakDown_InsertUpdate] " +
"@BD_ID = NULL,"+
"@FObject_ID = N'"+jsonobj.getString("FObject_ID")+"', " +
//생략
"@Description = N'"+jsonobj.getString("Description")+"'," +
"@User_ID = N'"+jsonobj.getString("User_ID")+"'";
boolean resut = st.execute(sql);
System.out.println(resut);
}
} catch (SQLException | ClassNotFoundException | JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.setStatus(HttpURLConnection.HTTP_FORBIDDEN);
}finally{
try {
if(st!=null) st.close();
if(connect!=null)connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
대충 이런식으로 구현하였고...
안드로이드쪽 은
public class HttpSendJSONTask extends AsyncTask<String, Void, String> {
public JSONArray jsonarr = new JSONArray();
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = sendJSONData(url);
Log.i("TAG", ".................................................."+output);
}
return output;
}
public String sendJSONData(String url){
HttpURLConnection conn = null;
try {
conn= (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(20000);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setDefaultUseCaches(false);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
wr.write(jsonarr.toString());
wr.flush();
Log.i("TAG", ".................................................."+conn.getResponseCode());
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return jsonarr.length()+"";
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
conn.disconnect(); //접속 종료
}
return "ERROR";
}
}
이런식으로 만들고 상속받아...derived class 에서....onpostExcute를 처리하였습니다.
서버를 확실하게 종료시키고 테스트하면 ERROR 스트링이 리턴되면서 동작을 하는데...
서버신호가 약할때(WIFI신호가 약할때) setItmeout 으로 처리를 해야하는거 같은데 이쪽 처리하는 방식을 잘모르겠습니다.
setConnecttime(초) setReadTime(초) 두개의 메서드로 조작을 해야할것 같은데 로직이 잘 떠오르질 않아서 문의들비니다
conn= (HttpURLConnection) new URL(url).openConnection();
conn.setConnectTimeout(20000);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setDefaultUseCaches(false);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
wr.write(jsonarr.toString());
wr.flush();
Log.i("TAG", ".................................................."+conn.getResponseCode());
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
return jsonarr.length()+"";
}
그러니까 이부분. 20000밀리초 후에 setconnecttimeout 을 하는데....
HTTP_OK 를 받지를 못했으니...경고창으로...10초정도 ok 를 받지 못하면...
네트워크가 원활하지 않습니다 다시시도해주세요.! 이런 경고창이 뜰수 있게 로직처리를 해야하는데...
좋은 방법이 있을까요?