我有 android 应用程序,它将图像转换为 base64 字符串。我想使用 Web 服务将此字符串发送到服务器。我正在使用 Jersey 的 REST Web 服务。
我可以从服务器发送和接收。我可以在 android 和服务器之间做任何事情。
现在:
当我将图像放入字符串中时,我得到了异常:
05-31 12:30:01.641: E/AndroidRuntime(19194): java.lang.OutOfMemoryError
很明显那是因为base64
字符串很大,但是如何解决那个pelase?
连接到服务器的类是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
public class Client {
private String server;
public Client(String server) {
this.server = server;
}
private String getBase() {
return server;
}
public String getBaseURI(String str) {
String result = "";
try {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpGet getRequest = new HttpGet(getBase() + str);
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
result = getResult(response).toString();
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}
private StringBuilder getResult(HttpResponse response) throws IllegalStateException, IOException {
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())), 1024);
String output;
while ((output = br.readLine()) != null)
result.append(output);
return result;
}
}
然后我只是像这样使用那个类:
Client client = new Client("http://192.168.55.1:8080/test2/eattel/");
String r = client.getBaseURI("image/" + "D");
看来我必须放入huge string
或getRequest
放入httpClient
但我不知道如何。
请有任何帮助
也许是因为我使用的是 HttpGet 而不是 HttpPost?