Android 애플리케이션에서 JSON을 AsyncHttp 프로토콜을 사용하여 sqlite 데이터를 원격 mysql 데이터베이스로 전달했습니다.
이제 MySQL 데이터베이스에서 SQLite 데이터베이스로 데이터를 가져 와서 서버에서 데이터를 가져 와서 JSON으로 변환합니다.
서버에 많은 행의 데이터가 있기 때문에 모바일 변수 sqlite 만 가져와 변수를 전달하고 모든 사용자 데이터를 가져 오는 것이 아니라 데이터가 광산인지 아닌지 확인해야합니다.android에서 URL을 통해 여러 매개 변수를 전달하고 PHP로 가져 오기
URL에서 변수를 전달하여 가져 오는 중에 사용자 데이터를 검색하고 SQLite 데이터베이스에 삽입하려는 경우 어떻게해야합니까?
여기 내 코드
public void syncMySQLDBSQLite(){
// Create AsycHttpClient object
AsyncHttpClient client = new AsyncHttpClient();
// Http Request Params Object
RequestParams params = new RequestParams();
// Show ProgressBar
prgDialog.show();
// Make Http call to getusers.php
client.post("http://10.0.2.2/tafapps/getuser.php", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
// Hide ProgressBar
prgDialog.hide();
// Update SQLite DB with response sent by getusers.php
updateSQLite(response);
}
// When error occured
@Override
public void onFailure(int statusCode, Throwable error, String content) {
// TODO Auto-generated method stub
// Hide ProgressBar
prgDialog.hide();
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]",
Toast.LENGTH_LONG).show();
}
}
});
}
public void updateSQLite(String response){
ArrayList<HashMap<String, String>> usersynclist;
usersynclist = new ArrayList<HashMap<String, String>>();
// Create GSON object
Gson gson = new GsonBuilder().create();
try {
// Extract JSON array from the response
JSONArray arr = new JSONArray(response);
Toast.makeText(getApplicationContext(), response, 5000).show();
System.out.println(arr.length());
// If no of array elements is not zero
if(arr.length() != 0){
// Loop through each array element, get JSON object which has userid and username
for (int i = 0; i < arr.length(); i++) {
// Get JSON object
JSONObject obj = (JSONObject) arr.get(i);
System.out.println(obj.get("userId"));
System.out.println(obj.get("userName"));
db = this.openOrCreateDatabase("Hangman", MODE_PRIVATE, null);
// String id = obj.get("userId").toString();
name = obj.get("userName").toString();
email = obj.get("userEmail").toString();
phn = obj.get("userPhnum").toString();
plyd = obj.get("userPlayed").toString();
crct = obj.get("userCorrect").toString();
//Toast.makeText(getApplicationContext(), email, 5000).show();
db.execSQL("insert into scores(userName,userEmail,userPhnum,userPlayed,userCorrect)values('"+name+"','"+email+"','"+phn+"','"+plyd+"','"+crct+"')");
}
//Toast.makeText(getApplicationContext(), ""+queryValues, 5000).show();
// Inform Remote MySQL DB about the completion of Sync activity by passing Sync status of Users
updateMySQLSyncSts(gson.toJson(usersynclist));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
당신이 얻을하는 방법을 설명하시기 바랍니다 수 있습니다 PHP 측면에서 URL에서 데이터 ..? –
PHP에서 요청 구문 분석이 설명되었습니다. – dasar
답변 해 주셔서 감사합니다. –