1

I have "BackUpContacts.db" database in SQLiteDatabase, it has a table named "ContactInfo" with column names ContactId, ContactName, MobilePhone1, MobilePhone2, OfficePhone1, OfficePhone2, OfficePhone3, HomePhone1, HomePhone2 and TokenId.

What i want is to transfer all data of "ContactInfo" table to the mysql database system at some server (means server has also a table similar to "ContactInfo", where all data of "ContactInfo" will be copied).

The last important thing which i want is that, whenever i want to get contacts(of a specified TokenId) i can backup all those from server to the mobile device in an xml file.

in short, can here anyone help me how to transfer sqlite db to a web server?

Wacek
  • 4,326
  • 2
  • 19
  • 28
Aditya Mehta
  • 811
  • 3
  • 9
  • 8

3 Answers3

2

One way is to submit data to your webiste's php page by using GET or POST method which will add it to MySql on your remote application server, like

    HttpGet request = new HttpGet(url); 
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    HttpClient client = new DefaultHttpClient();
    String response = client.execute(request, responseHandler);

url can be: www.mysite.com?id=5&name=john ... This also has to be an AsyncTask doInBackground request... The problem is how many records can be uploaded at once...

It easy to make the remote application server produce an XML file on request to be downloaded by android, with a similar request like above.

Lumis
  • 21,517
  • 8
  • 63
  • 67
  • Thanks for the reply ChristianB, could you (or anyone) elaborate how to send xml file to the web service. (Because i stored all the data of "ContactInfo" table in a "contacts.xml" file, now i want to send this xml file to the web service) – Aditya Mehta Jan 15 '11 at 12:14
0

This is an example how to upload an XML file from http://w3mentor.com/learn/java/android-development/android-http-services/example-of-multipart-post-using-android/ Where data.xml file and two more fields ("one" and "two") are uploaded to the server. Note that this will require additional jar libraries which you need to download and place in the "lib" folder on the same level as "res" and and "src" folders in the project. Here are the three jar files you need httpmime-4.0.jar, apache-mime4j-0.6.jar, commons-io-1.4.jar:

http://james.apache.org/download.cgi#Apache_Mime4J

https://repository.apache.org/content/repositories/releases/org/apache/httpcomponents/httpmime/4.0.1/httpmime-4.0.1.jar

http://code.google.com/p/mapmap/downloads/detail?name=commons-io-1.4.jar&can=2&q=

You add this to the project by selecting the project in Exclipse and click File>Properties>Java Build Path>Libraries and then [Add jars]

    import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
public class TestMultipartPost extends Activity
{
    public void executeMultipartPost()throws Exception
    {
        try {
            InputStream is = this.getAssets().open("data.xml");
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://w3mentor.com/Upload.aspx");
            byte[] data = IOUtils.toByteArray(is);
            InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data),"uploadedFile");
            StringBody sb1 = new StringBody("someTextGoesHere");
            StringBody sb2 = new StringBody("someTextGoesHere too");
            MultipartEntity multipartContent = new MultipartEntity();
            multipartContent.addPart("uploadedFile", isb);
            multipartContent.addPart("one", sb1);
            multipartContent.addPart("two", sb2);
            postRequest.setEntity(multipartContent);
            HttpResponse res = httpClient.execute(postRequest);
            res.getEntity().getContent().close();
        } catch (Throwable e)
        {
            // handle exception here
        }
    }
}

One more thing, you need to run this in the "background" as an asynchronous task like this:

    private class XmlUploadTask extends AsyncTask<Object, String, Boolean> {
    private static final String DEBUG_TAG = "XmlUploadTask";
    ProgressDialog pleaseWaitDialog;

    @Override
    protected void onCancelled() {
        Log.i(DEBUG_TAG, "onCancelled");
        pleaseWaitDialog.dismiss();
    }

    @Override
    protected void onPostExecute(Boolean result) {
        Log.i(DEBUG_TAG, "onPostExecute");
        pleaseWaitDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        pleaseWaitDialog = ProgressDialog.show(myActivity.this, "My Application", "Uploading data...", true, true);
        pleaseWaitDialog.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface dialog) {
                XmlUploadTask.this.cancel(true);
            }
        });
    }

    @Override
    protected Boolean doInBackground(Object... params) {
//here you enter the xml upload code above
         return null;
    }

}
Lumis
  • 21,517
  • 8
  • 63
  • 67
  • ChristianB thanks one more time, you explained very well, everything is precise. Can you tell bit more about the second parameter "uploadedFile" of ByteArrayInputStream() constructor, what actually it is doing?, it is also used in 'multipartContent.addPart("uploadedFile", isb);'. is it something at server side (or at web service, whatever). – Aditya Mehta Jan 17 '11 at 07:11
  • Actually i do not have idea how to use web services, i am doing this first time. The example you mentioned has helped me to understand how to send request, thanks for that one more time. :) – Aditya Mehta Jan 17 '11 at 07:22
  • "uploadedFile" is just a name which Java method requires when converting a file into a sutable form for upload. This is all at android side. The second occurance of "uploadedFile" will provide the webiste with a reference or name of the object which contains the file which is being submited. Then this object can be processed and saved on the server disk. – Lumis Jan 17 '11 at 23:19
  • http://www.w3schools.com/PHP/php_file_upload.asp in this link there is a code how to upload a file, though this is about an image upload. Note that android would do the same as is the "Create an Upload-File Form" web page. You need therefore to understand how the rest works i.e. upload_file.php with the "Saving the Uploaded File" code. PHP is a bit easier than Java on android... here is tutorial how to open and read xml after it is saved: http://www.kirupa.com/web/xml_php_parse_beginner.htm – Lumis Jan 17 '11 at 23:40
  • You can learn PHP in a day if you go to lynda.com and pay for a month subcription. It has powerful professional video tutorials about most software, it did miracles for me, it is like downloding knowledge into the brain. – Lumis Jan 17 '11 at 23:48
  • Is there an entity other than "MultipartEntity" which i can pass in "postRequest.setEntity(HttpEntity entity)"? – Aditya Mehta Jan 18 '11 at 05:28
  • I think you would have do some research on android multipart post, there are seem to be different solutions. If you are sending just text, like pairs of name plus value in simple text, then you don't need multipart and additional java libraries. I also don't know the multipart capabilities of android 2.3 If you are sending just one file perhaps this code would work: http://stackoverflow.com/questions/3038409/how-to-send-http-post-request-and-recieve-response/3038747#3038747 – Lumis Jan 18 '11 at 09:47
0

Another way which i found over web that how to send xml file to a web server: For this you need to add a jar file "commons-httpclient.jar" in your project. (How to add jar file is clearly mentioned by ChristianB in above/below post)

You can download this jar file from http://www.java2s.com/Code/Jar/ABC/Downloadcommonshttpclientjar.htm 2.code would be...

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
    import org.apache.commons.httpclient.methods.PostMethod;

    import java.io.File;
    import java.io.FileInputStream;

    class ClassName extends Activity
    {
         public void Sync(View v) // on button click
         {
            File xmlFile = new File("sdcard/contacts.xml");
            FileInputStream fis = new FileInputStream(xmlFile);
            InputStreamRequestEntity isre = new InputStreamRequestEntity(fis);

                /*now pass url of server in constructor of PostMethod*/
            PostMethod post = new PostMethod("http://w3mentor.com/Upload.aspx");
            post.setRequestEntity(isre);
            post.setRequestHeader("Content-Type", "text/xml");

            HttpClient httpclient = new HttpClient();
            int response = httpclient.executeMethod(post);
            String res = post.getResponseBodyAsString();
            Toast.makeText(GetContacts.this, new Integer(response).toString()+" "+res, Toast.LENGTH_LONG).show();
             post.releaseConnection();
         }
     }
Aditya Mehta
  • 811
  • 3
  • 9
  • 8
  • Above example is taken from: http://www.java-tips.org/other-api-tips/httpclient/how-to-send-an-xml-document-to-a-remote-web-server-using-http-5.html – Aditya Mehta Jan 18 '11 at 09:15