2

I'm trying to send a sqlite database from my android phone to a web server. I get no errors when the code executes, however the database doesn't appear on the server. Here is my php code and code to upload the file from the android phone. The connection response message is get is "OK" and the response from the http client I get is org.apache.http.message.BasicHttpResponse@4132dd40.

    public void uploadDatabase() {


    String urli = "http://uploadsite.com";
                String path = sql3.getPath();
                File file = new File(path);

            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1*1024*1024;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";


            try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(urli);

            URL url = new URL(urli);
            connection = (HttpURLConnection) url.openConnection();

            InputStreamEntity reqEntity = new InputStreamEntity(
                    new FileInputStream(file), -1);

            reqEntity.setContentType("binary/octet-stream");
            reqEntity.setChunked(true);

            HttpResponse response = httpclient.execute(httppost);
            String response2 = connection.getResponseMessage();
            Log.i("response", response.toString());
            Log.i("response", response2.toString());
        } catch (Exception e) {

        }
    }


<?php
$uploaddir = '/var/www/mvideos/uploads/';
$file = basename($_FILES['userfile']['name']);
$timestamp = time();
$uploadfile = $uploaddir . $timestamp . '.sq3';

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "OK";
} else {
    echo "ERROR: $timestamp";
}

?>
2
  • Did you get this to work. If so how. I need to do the same thing. Commented May 9, 2012 at 19:25
  • Yep I don't have the exact code but I put a sample below of what I based it off of. Commented May 10, 2012 at 18:27

1 Answer 1

1

I based my code on this example and it worked fine.

String pathToOurFile = "/data/dada.jpg"; 
String urlServer = "http://sampleserver.com";

try {
    FileInputStream fis = new FileInputStream(new File(pathToOurFile));
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost(urlServer);
    byte[] data = IOUtils.toByteArray(fis);
    InputStreamBody isb = new InputStreamBody(new ByteArrayInputStream(data),pathToOurFile);
    StringBody sb1 = new StringBody("someTextGoesHere");
    StringBody sb2 = new StringBody("someTextGoesHere too");
    MultipartEntity multipartContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    FileBody bin = new FileBody(new File(pathToOurFile));

    multipartContent.addPart("uploadedfile", bin);
    multipartContent.addPart("name", sb1);
    multipartContent.addPart("status", sb2);
    postRequest.setEntity(multipartContent);

    HttpResponse res = httpClient.execute(postRequest);
    res.getEntity().getContent().close();
} catch (Throwable e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.