연락처를 삭제하는 REST 애플리케이션을 개발 중이며 (원시 주소록이 아닌 연락처) 대량으로 개발하고 있습니다. ~ 116 연락처를 삭제하는 데 3 분 이상 걸립니다. 제 의견으로는 많은 방법입니다. 때로는 Contacts deleted
을 인쇄하지만 여전히 휴대 전화의 연락처 목록에 있으며 실제로 삭제 된 다른 시간도 있습니다.주소 일괄 처리/일괄 삭제 -
누구나 여기서 문제를 지적 할 수 있습니까?
연락처 ID (및 몇 가지 다른 세부 정보가 포함되어 있지만이 방법에서는 ID 만 사용됨)가 포함 된 JSONArray
가 수신되어 삭제됩니다. 나는 deleteBatchContact
메서드를 호출 할 경우
public Boolean deleteBatchContact(final JSONArray jsonArray) throws JSONException, RemoteException, OperationApplicationException
{
final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
final Long startTime = System.currentTimeMillis();
if(jsonArray.length() != 0) // there must be something in the JSONArray
{
for(int i = 0; i < jsonArray.length(); i++)
{
final JSONObject jsonContactObject = jsonArray.getJSONObject(i);
final String contactId = jsonContactObject.getString("id");
Long id = Long.parseLong(contactId);
ops.add(ContentProviderOperation.newDelete(ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.parseLong(contactId)))
.withYieldAllowed(true)
.build());
}
try {
final ContentProviderResult[] cpr = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
final Long endTimeTry = System.currentTimeMillis();
final Integer numberOfContactsDeleted = cpr.length;
if(numberOfContactsDeleted > 0)
{
Log.d(TAG, numberOfContactsDeleted + " Contacts deleted!");
final Long totalSuccess = endTimeTry-startTime;
Log.d(TAG, "Total Time (Sucess): " + totalSuccess);
return true;
}
else
{
Log.d(TAG, "Menor ou igual a zero...");
final Long totalFailed = endTimeTry-startTime;
Log.d(TAG, "Total Time (No deletes): " + totalFailed);
return false;
}
} catch (Exception e) {
Log.d(TAG, "deleteBatchContact: " + e.getMessage());
return false;
}
}
Long endTimeReturnFalse = System.currentTimeMillis();
Long totalTimeReturnFalse = endTimeReturnFalse - startTime;
Log.d(TAG, "Total time return false: " + totalTimeReturnFalse);
return false;
}
그리고 여기에 있습니다 : :
가 여기 내 삭제 연락처 코드의
02-13 01:28:04.484: D/ContactRestlet(17638): DELETE REQUEST
02-13 01:28:04.507: D/ContactRestlet(17638): age1: 0
02-13 01:28:04.507: D/ContactRestlet(17638): is commited1? false
02-13 01:28:04.507: D/ContactRestlet(17638): Try...
02-13 01:30:04.671: D/ContactRestlet(17638): DELETE REQUEST
02-13 01:30:04.671: D/ContactRestlet(17638): age1: 0
02-13 01:30:04.671: D/ContactRestlet(17638): is commited1? false
02-13 01:30:04.671: D/ContactRestlet(17638): Try...
02-13 01:31:07.468: D/ContactList(17638): 116 Contacts deleted!
02-13 01:31:07.468: D/ContactList(17638): Total Time (Sucess): 182911
02-13 01:31:07.468: D/ContactRestlet(17638): Response -> Deleted with Success!
02-13 01:31:07.472: D/ContactRestlet(17638): age2: 0
02-13 01:31:07.472: D/ContactRestlet(17638): is commited2? false
02-13 01:31:07.476: W/System.err(17638): 2014-02-13 01:31:07 - - - 8080 DELETE /contacts - 1001 21 12590 62837 http://10.17.1.72:8080 Apache-HttpClient/4.3.1 (java 1.5) -
편집 : 여기
else if(type.equalsIgnoreCase("delete"))
{
Log.d(TAG, "DELETE REQUEST");
String strJson = request.getEntityAsText();
int age = response.getAge();
Log.d(TAG, "age1: " + age);
Log.d(TAG, "is commited1? " + response.isCommitted());
try {
Log.d(TAG, "Try...");
JSONArray jsonArray = new JSONArray(strJson);
if(processDelete(jsonArray) == true)
{
Log.d(TAG, "Response -> Deleted with Success!");
response.setEntity("Deleted with success!", MediaType.TEXT_ALL);
age = response.getAge();
Log.d(TAG, "age2: " + age);
Log.d(TAG, "is commited2? " + response.isCommitted());
}
else
{
Log.d(TAG, "Response -> No contacts deleted...");
response.setEntity("No contacts deleted...", MediaType.TEXT_ALL);
age = response.getAge();
Log.d(TAG, "age3: " + age);
Log.d(TAG, "is commited3? " + response.isCommitted());
}
} catch (RemoteException e) {
Log.d(TAG, "processDelete exception: " + e.getMessage());
e.printStackTrace();
} catch (JSONException e) {
Log.d(TAG, "processDelete exception: " + e.getMessage());
e.printStackTrace();
} catch (OperationApplicationException e) {
Log.d(TAG, "processDelete exception: " + e.getMessage());
e.printStackTrace();
}
}
는 출력 @ RocketRandom, 여기에 나의 새로운 코드 :
public Boolean deleteBatchContact(final JSONArray jsonArray) throws JSONException, RemoteException, OperationApplicationException
{
final ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
final Long startTime = System.currentTimeMillis();
if(jsonArray.length() != 0) // there must be something in the JSONArray
{
StringBuilder query = new StringBuilder(" in {");
for(int i = 0; i < jsonArray.length(); i++)
{
final JSONObject jsonContactObject = jsonArray.getJSONObject(i);
final String contactId = jsonContactObject.getString("id");
query.append(contactId).append(",");
}
query.deleteCharAt(query.length()-1);
query.append("}");
ops.add(ContentProviderOperation.newDelete(Contacts.CONTENT_URI)
.withSelection(Contacts._ID + " in { 1 , 2 , 3 , 4 , 5, 6, 7, 8, 9}", null).build());
try {
final ContentProviderResult[] cpr = contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
final Long endTimeTry = System.currentTimeMillis();
final Integer numberOfContactsDeleted = cpr.length;
if(numberOfContactsDeleted > 0)
{
Log.d(TAG, numberOfContactsDeleted + " Contacts deleted!");
final Long totalSuccess = endTimeTry-startTime;
Log.d(TAG, "Total Time (Sucess): " + totalSuccess);
return true;
}
else
{
Log.d(TAG, "Menor ou igual a zero...");
final Long totalFailed = endTimeTry-startTime;
Log.d(TAG, "Total Time (No deletes): " + totalFailed);
return false;
}
} catch (Exception e) {
Log.d(TAG, "deleteBatchContact: " + e.getMessage());
return false;
}
}
Long endTimeReturnFalse = System.currentTimeMillis();
Long totalTimeReturnFalse = endTimeReturnFalse - startTime;
Log.d(TAG, "Total time return false: " + totalTimeReturnFalse);
return false;
}
그리고 이것은 출력 : 이제
03-06 01:42:10.367: D/ContactList(8925): 1 Contacts deleted!
03-06 01:42:10.367: D/ContactList(8925): Total Time (Sucess): 84
는, 당신은 오류가 어디 자리 수있는 연락처를 삭제 아니에요? 심지어 그것은 하나의 연락처가 삭제되었다고 말했고, 삭제 된 사람은 없다고 말했습니다. 모든 연락처 ID가 유효합니다 (연락처 테이블에 존재하고 기기에서 다운로드하여 확인했습니다.)
안드로이드는 이러한 유형의 쿼리를 SQL Lite 데이터베이스에 직접 들여 보내지 않습니다. – dazito
나는 android가 제공하는 API를 사용하여 유사한 쿼리를 작성하려고합니다. 위의 쿼리를 통합하기 위해 where 문자열을 조정할 수 있습니다. 예를 들어 업데이트 된 게시물. – RocketRandom
수정 된 질문을 확인하십시오. 이제는 연락처를 삭제하지 않습니다. – dazito