回答

0

要獲得所有聯繫人:

ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
       null, null, null, null); 
     if (cur.getCount() > 0) { 
      while (cur.moveToNext()) { 
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        if (Integer.parseInt(cur.getString(
         cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
        Cursor pCur = cr.query(
           ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
           null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
           new String[]{id}, null); 
        while (pCur.moveToNext()) { 
         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show(); 
        } 
        pCur.close(); 
       } 
      } 
     } 

For more information.

上傳數據:

public class HTTPHelp{ 

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpContext localContext = new BasicHttpContext(); 
private boolean abort; 
private String ret; 

HttpResponse response = null; 
HttpPost httpPost = null; 

public HTTPHelp(){ 

} 

public void clearCookies() { 

    httpClient.getCookieStore().clear(); 

} 

public void abort() { 

    try { 
     if(httpClient!=null){ 
      System.out.println("Abort."); 
      httpPost.abort(); 
      abort = true; 
     } 
    } catch (Exception e) { 
     System.out.println("HTTPHelp : Abort Exception : "+e); 
    } 
} 

public String postPage(String url, String data, boolean returnAddr) { 

    ret = null; 

    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); 

    httpPost = new HttpPost(url); 
    response = null; 

    StringEntity tmp = null;   

    httpPost.setHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux " + 
     "i686; en-US; rv:1.8.1.6) Gecko/20061201 Firefox/2.0.0.6 (Ubuntu-feisty)"); 
    httpPost.setHeader("Accept", "text/html,application/xml," + 
     "application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); 
    httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); 

    try { 
     tmp = new StringEntity(data,"UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     System.out.println("HTTPHelp : UnsupportedEncodingException : "+e); 
    } 

    httpPost.setEntity(tmp); 

    try { 
     response = httpClient.execute(httpPost,localContext); 
    } catch (ClientProtocolException e) { 
     System.out.println("HTTPHelp : ClientProtocolException : "+e); 
    } catch (IOException e) { 
     System.out.println("HTTPHelp : IOException : "+e); 
    } 
      ret = response.getStatusLine().toString(); 

      return ret; 
      } 
} 


可以reffer:http://hmkcode.com/android-send-json-data-to-server/

+0

對不起,第二個代碼是什麼,我是一個小新手,以解釋我正在做什麼 – plus 2014-09-03 14:26:25

+0

你可以搜索[DefaultHttpClient](http://developer.android.com/reference/org/apache/http/impl/客戶端/ DefaultHttpClient.html),[HttpContext](http://developer.android.com/reference/org/apache/http/protocol/HttpContext.html),你應該通過一些例子[http://hmkcode.com /android-internet-connection-using-http-get-httpclient/](http://hmkcode.com/android-internet-connection-using-http-get-httpclient/),[androidhive](http:// www .androidhive.info/2011/10/android-making-http-requests /)或[hayageek](http://hayageek.com/android-http-post-get/) – 2014-09-03 14:49:13

+0

是否存在將聯繫人發送到服務器的源? – plus 2014-09-04 08:08:30

相關問題