mysql과 안드로이드 데이터 I/O과정에서 jsp/JSON을 공부하고 있습니다.
그런데
string strid = joUser.get("id").tosting();은 스트링형으로 변환인데
int hashcode = Integer.parseInt(joUser.get("hashcode").tosting()); 스트링으로 받아서 다시 인트로 파싱하는 이유가 무엇입니까?
int hashcode = joUser.get("hashcode").getInt(); 처럼 인티져로 바로 받으면 안 되는 건가요?
참고로 hash값은 안드로이드 sql둘다 숫자로 들어가고 있습니다.
밑에는 예제 소스입니다.
Export.java
private boolean exportDb()
{
String strUrl = strServerIp + "vbns_export.jsp";
ArrayList<String> arraylistVbns = new ArrayList<String>();
VBNSDbHandler dbhandler = VBNSDbHandler.open(VBNSBackup.this);
dbhandler.selectAll(arraylistVbns);
dbhandler.close();
JSONObject joInput = new JSONObject();
try {
joInput.put("memberid", strMemberId);
JSONArray jaDb = new JSONArray();
for(int i=0; i < arraylistVbns.size(); i++)
{
JSONObject joData = new JSONObject();
String[] strMsg = arraylistVbns.get(i).split(";");
int hashcode = Integer.valueOf(strMsg[0]);
int second = Integer.valueOf(strMsg[1]);
String strMemo = strMsg[2];
joData.put("hashcode", hashcode);
joData.put("second", second);
joData.put("memo", strMemo);
jaDb.put(joData);
}
joInput.put("data", jaDb);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpClient client = new DefaultHttpClient();
HttpPost post=new HttpPost(strUrl);
ArrayList<BasicNameValuePair> name=new ArrayList<BasicNameValuePair>();
name.add(new BasicNameValuePair("inputdata", joInput.toString()));
UrlEncodedFormEntity entity = null;
try {
entity = new UrlEncodedFormEntity(name, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
post.setEntity(entity);
HttpResponse responsePOST = null;
try {
responsePOST = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(responsePOST.getStatusLine().getStatusCode()==200)
return true;
else
return false;
}
}
--------------------------
export.jsp
if(strReq == null)
out.print("잘못된 접근입니다");
else
{
String url="jdbc:mysql://localhost/vbns";
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url,"root","kwcvbns");
Statement stmt=conn.createStatement();
String SQL = null;
JSONObject joReq = (JSONObject)JSONValue.parse(strReq);
String strMemberId = joReq.get("memberid").toString();
SQL = "UPDATE member SET backup = 1 WHERE id = '" + strMemberId + "';";
stmt.executeUpdate(SQL);
SQL = "DELETE FROM backup WHERE memberid = '" + strMemberId + "';";
stmt.executeUpdate(SQL);
JSONArray jaData = (JSONArray)joReq.get("data");
for(int i=0; i < jaData.size(); i++)
{
JSONObject joValue = (JSONObject)jaData.get(i);
int hashcode = Integer.valueOf(joValue.get("hashcode").toString());
int second = Integer.valueOf(joValue.get("second").toString());
String strMemo = joValue.get("memo").toString();
SQL = "INSERT INTO backup VALUES(" + hashcode + ", " + second + ", '" + strMemo + "', '" + strMemberId + "');";
stmt.executeUpdate(SQL);
}
stmt.close();
conn.close();
}