所以我有一个代码,我可以使用以下代码使用 HTTP POST 将数据发送到服务器:
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
public void sendData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SERVER_URL);
Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data",Base64.encodeBytes(data)));
nameValuePairs.add(new BasicNameValuePair("id", "123"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (Exception e) {
}
}
问题是,通过这种实现,我无法发送大数据,因为字节数组必须全部放入内存中。在 Android 中通过 HTTP POST 发送大数据的最佳方式是什么,最好没有第三方库?