我有一个REST服务,我无法修改,上传图像的方法以Base64字符串编码。制作大型REST请求
问题是,图像可能会达到5-10MB的大小,也许更多。当我尝试在设备上构建此大小的图像的Base64表示形式时,出现OutOfMemory异常。
我可以一次然而编码的字节块(3000比方说),但是这没用,因为我需要整个字符串创建一个HTTPGET/HttpPost对象:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("www.server.com/longString");
HttpResponse response = client.execute(httpGet);
是否有办法这样做吗?我想在下面一行发现一个异常(“java.io.FileNotFoundException:http://www.google.com”):InputStream in = urlConnection.getInputStream();
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
out.write("/translate".getBytes());
InputStream in = urlConnection.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
System.out.println("response:" + total);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我错过了什么吗?我需要执行的GET请求如下所示: “http://myRESTService.com/myMethod?params=LOOONG-String”,所以想法是连接到http://myRESTService.com/myMethod,然后一次输出长字符串的几个字符。它是否正确?
你需要告诉服务使用put或post等。通过con.setMethod(“POST”) –