mercredi 9 septembre 2015

I want to retrieve the contacts from my android phone and save them to mysql database

This is what I have done to retrieve the contacts.`

public class MainActivity extends Activity 
 {
    private static final String TAG = MainActivity.class.getSimpleName();
    TextView tv;
    private TextView status,role,method;

    @Override
    protected void onCreate(Bundle savedInstanceState)

    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.text1);
        //use cursor to keep any type of data
        //take all mobile contacts database in cursor
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.
                Phone.CONTENT_URI, null, null, null, null);
        while (phones.moveToNext()) {
            //get name and number from cursor using column index
            String name = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.NUMBER));

            tv.append(name + "->" + phoneNumber + "\n");

            new SigninActivity(this, status, role,1).execute(name, phoneNumber);

        }
        phones.close();

    }
 }`

And the SigninActivity looks like

public class SigninActivity  extends AsyncTask<String,Void,String>{
private TextView statusField,roleField;
private Context context;
private int byGetOrPost = 0;

//flag 0 means get and 1 means post.(By default it is get.)
public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
    this.context = context;
    this.statusField = statusField;
    this.roleField = roleField;
    byGetOrPost = flag;
}

protected void onPreExecute(){

}

@Override
protected String doInBackground(String... arg0) {
    if(byGetOrPost == 0){ //means by Get Method

        try{
            String username = (String)arg0[0];
            String password = (String)arg0[1];
            String link = "http://ift.tt/1se8HDw"+username+"& password="+password;

            URL url = new URL(link);
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(link));
            HttpResponse response = client.execute(request);
            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer sb = new StringBuffer("");
            String line="";

            while ((line = in.readLine()) != null) {
                sb.append(line);
                break;
            }
            in.close();
            return sb.toString();
        }

        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }
    else{
        try{
            String username = (String)arg0[0];
            String password = (String)arg0[1];

            String link="http://ift.tt/1H7GiJk";
            String data  = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
            data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");

            URL url = new URL(link);
            URLConnection conn = url.openConnection();

            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

            wr.write( data );
            wr.flush();

            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line);
                break;
            }
            return sb.toString();
        }
        catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }
}

@Override
protected void onPostExecute(String result){
    this.statusField.setText("Login Successful");
    this.roleField.setText(result);
}
}

I have used post method to send it to my server but whenever I run the app it throws some exception of sqlite, so far I am able to just see the contact list in the list view but I am not able to save them to database. I might be completely wrong in the methodology is there any other way to store contact information to mysql?

myLogcat error

09-09 14:25:12.015    3324-3324/? E/HwLauncher﹕ Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
09-09 14:25:12.680    3324-3324/? E/HwLauncher﹕ Launcher dialog dismiss failed : java.lang.IllegalArgumentException: no dialog with id 1 was ever shown via Activity#showDialog
09-09 14:29:32.525    2919-3209/? E/SQLiteDatabase﹕ Error inserting loc_source=network areacode=37011 time=1441789172517 longitude=75.7906258 cellid=14156062 latitude=26.8565597 accuracy=51.0
android.database.sqlite.SQLiteConstraintException: column cellid is not unique (code 19)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.android.server.location.LocalLocationDBHelper.insert(LocalLocationDBHelper.java:80)
at com.android.server.location.LocalLocationManager.insertLocToDB(LocalLocationManager.java:606)
at com.android.server.location.LocalLocationManager.refreshLocToCellTable(LocalLocationManager.java:404)
at com.android.server.location.LocalLocationManager.access$1200(LocalLocationManager.java:61)
at com.android.server.location.LocalLocationManager$RefreshCellInfoDBTask.doInBackground(LocalLocationManager.java:792)
at com.android.server.location.LocalLocationManager$RefreshCellInfoDBTask.doInBackground(LocalLocationManager.java:778)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

enter code here

Aucun commentaire:

Enregistrer un commentaire