java - 试图将 sqlite 数据库从 android 手机发送到 web 服务器

标签 java android database upload

我正在尝试将 sqlite 数据库从我的 android 手机发送到网络服务器。代码执行时我没有收到任何错误,但是数据库没有出现在服务器上。这是我的 php 代码和从 android 手机上传文件的代码。连接响应消息是“确定”,我收到的 http 客户端的响应是 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";
}

?>

最佳答案

我的代码基于此示例,它运行良好。

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();
}

关于java - 试图将 sqlite 数据库从 android 手机发送到 web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10272300/

相关文章:

android - DBFlow 一对多示例

Java - 如何在冒泡排序后将值打印为单词?

java - 如何从另一个子类创建一个对象?

java - 可以确定要解压缩的文件的大小吗?

php - mySQL 加入哪里?

sql - 如何对数据中包含 '%'的列执行Like in SQL?

java - 拆分一个类是否可以更轻松地测试可接受的行为?

java - 是 6.1,Java 5 : Interesting Issue - Request with word 'CD' not reaching the servlet

java - 如何存储和检索 RecyclerView 项目以供离线使用

java - 文本没有出现但存在,尝试制作一个计算平均值的简单应用程序