从Android设备向服务器发送大量数据

时间:2014-06-18 07:56:52

标签: php android json httprequest

我需要将我的Android设备中的csv中的大量数据发送到我服务器上的网页,以便在我的php上构建大量的SQL查询。

很多人谈到使用JSON for android,以及其他人通过使用类似的方式发送一个字符串数组数组:

String dataPOST = URLEncoder.encode("table") + "=" + URLEncoder.encode(tableName) + "&"     + URLEncoder.encode("data") + "="
                + URLEncoder.encode(sb.toString())+ "&" + URLEncoder.encode("idvente") + "="
                        + URLEncoder.encode(idvente);

        URL url = new URL(link);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

        wr.write(dataPOST);
        wr.flush();

但是我担心php /我的服务器的限制因为它可能是很多数据。 什么是我的问题的最佳解决方案?

谢谢你,对不起英语中的错误,我不会流利xD!

1 个答案:

答案 0 :(得分:0)

您可以使用MultipartEntity发出POST请求。您可以将任何字符串和文件放入请求正文:

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);

try {
    HttpPost httpPost = new HttpPost(YOUR_URL);

    MultipartEntity multipartEntity = new MultipartEntity();
    multipartEntity.addPart("param1", new StringBody("param1 value", Charset.forName("UTF-8")));
    multipartEntity.addPart("param2", new StringBody("param2 value", Charset.forName("UTF-8")));

    if (imagePath.length() > 0) {
        multipartEntity.addPart("images", new FileBody(new File(imagePath)));
    }

    httpPost.setEntity(multipartEntity);

    String resp = httpClient.execute(httpPost, new BasicResponseHandler());
} catch (Exception e) {
}

您也可以尝试UrlEncodedFormEntity。它也没有字符串长度的限制。

HttpPost httpPost = new HttpPost(sb.toString());

List<NameValuePair> entityParams = new ArrayList<NameValuePair>();
entityParams.add(new BasicNameValuePair("action", "postcomment"));
entityParams.add(new BasicNameValuePair("app_id", com.appbuilder.sdk.android.Statics.appId));
entityParams.add(new BasicNameValuePair("token", com.appbuilder.sdk.android.Statics.appToken));
entityParams.add(new BasicNameValuePair("module_id", Statics.MODULE_ID));
entityParams.add(new BasicNameValuePair("parent_id", pVideoItem.getId() + ""));

httpPost.setEntity(new UrlEncodedFormEntity(entityParams, "utf-8"));

String resp = httpClient.execute(httpPost, new BasicResponseHandler());