Java Android 2.1似乎没有刷新输出流
我们在安卓系统中发现了一些非常奇怪的行为。我们的网络堆栈(与REST服务器通信)在几乎所有情况下都能正常工作,但在完成较大的帖子后不久执行GET时除外。似乎正在发生的是,输出流没有刷新,并在新套接字打开时发送最后一行。请注意,每个连接都是一个新创建的对象,因此这是意外行为。首先,似乎将我指向输出流的错误代码,它们来自服务器日志 10.1.8.195---[07/Nov/2012:13:36:28-0700]“POST/iou/lender HTTP/1.1“200 28”-“Android” 10.1.8.195---[07/Nov/2012:13:36:36-0700]“V2ymHFg03ehbqgZCaKO6jy”400 173“-” 之后的尝试应该是GET,然后从服务器中提取数据,其中包括通过POST添加的新条目。然而,我们得到的又是POST输出流的最后一行。这是我们网络堆栈的核心代码,如果需要更多的相关代码,请告诉我Java Android 2.1似乎没有刷新输出流,java,android,http,rest,Java,Android,Http,Rest,我们在安卓系统中发现了一些非常奇怪的行为。我们的网络堆栈(与REST服务器通信)在几乎所有情况下都能正常工作,但在完成较大的帖子后不久执行GET时除外。似乎正在发生的是,输出流没有刷新,并在新套接字打开时发送最后一行。请注意,每个连接都是一个新创建的对象,因此这是意外行为。首先,似乎将我指向输出流的错误代码,它们来自服务器日志 10.1.8.195---[07/Nov/2012:13:36:28-0700]“POST/iou/lender HTTP/1.1“200 28”-“Android” 1
public Object serverConnect(String url, String method,
Hashtable<String, Object> params) {
HttpConnection c = null;
InputStream is = null;
OutputStream out = null;
ByteArrayOutputStream postDataByteArrayImage = new ByteArrayOutputStream();
byte[] data;
String boundry = "----------V2ymHFg03ehbqgZCaKO6jy";
try {
if (!url.startsWith("/")) {
url = "/" + url;
}
String uri = Control.URL_Secure + Control.dtserver + ":"
+ Control.port + url;
ByteArrayOutputStream postDataByteArray = new ByteArrayOutputStream();
params.put("sessionId", Control.sessionId);
if (method.equals("GET")) {
uri = uri + "?";
Enumeration enumParams = params.keys();
while (enumParams.hasMoreElements()) {
if (!uri.endsWith("?")) {
uri = uri + "&";
}
String key = (String) enumParams.nextElement();
uri = uri
+ key
+ "="
+ java.net.URLEncoder.encode((String) params
.get(key));
}
} else if (method.equals("POST")) {
Enumeration enumParams = params.keys();
postDataByteArray.write(("--").getBytes());
postDataByteArray.write((boundry).getBytes());
postDataByteArray.write(("\r\n").getBytes());
while (enumParams.hasMoreElements()) {
String key = (String) enumParams.nextElement();
if (!key.equals("image")){
postDataByteArray
.write(("Content-Disposition: form-data; name=\"")
.getBytes());
postDataByteArray.write((key).getBytes());
postDataByteArray.write(("\"").getBytes());
postDataByteArray.write(("\r\n\r\n").getBytes());
postDataByteArray.write(((String) params.get(key))
.getBytes());
postDataByteArray.write(("\r\n").getBytes());
postDataByteArray.write(("--").getBytes());
postDataByteArray.write(boundry.getBytes());
postDataByteArray.write(("\r\n").getBytes());
}
}
postDataByteArray.close();
}
Log.i("URL", uri);
URL urltoConenct = new URL(uri);
URLConnection connection = urltoConenct.openConnection();
HttpURLConnection urlConnection = (HttpURLConnection) connection;
URLConnection.setDefaultRequestProperty("Method", method); // default
urlConnection.setRequestProperty("User-Agent", "Android");
if (method.equals("POST")) {
urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(postDataByteArray.toByteArray().length + postDataByteArrayImage.toByteArray().length);
urlConnection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundry);
out = urlConnection.getOutputStream();
out.write(postDataByteArray.toByteArray());
out.write(postDataByteArrayImage.toByteArray());
out.close();
}
int response = 0;
try {
response = urlConnection.getResponseCode();
} catch (IOException e) {
if (e.toString()
.equals("java.io.IOException: Received authentication challenge is null"))
throw new RESTException(401, "Invalid Phone or Pin");
else
throw e;
}
if (response == HttpURLConnection.HTTP_OK) {
is = urlConnection.getInputStream();
if (is == null) {
return new IOException(
"Cannot open HTTP InputStream, aborting");
}
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int ch;
int count = 0;
while ((ch = is.read()) != -1) {
bo.write(ch);
count++;
}
data = bo.toByteArray();
return new String(data);
} else if (response == 500) {
return new RESTException(500, "Internal server error");
} else {
RESTException x = new RESTException();
x.setCode(response);
try {
is = urlConnection.getInputStream();
if (is == null) {
x.setMessage("Unable to retrieve message");
return x;
}
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int ch;
int count = 0;
while ((ch = is.read()) != -1) {
bo.write(ch);
count++;
}
data = bo.toByteArray();
String output = new String(data);
JSONObject obj;
try {
obj = new JSONObject(output);
JSONObject err = obj.getJSONArray("errors")
.getJSONObject(0);
x.setMessage(err.getString("message"));
} catch (JSONException e) {
Log.e("stuff", output);
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// Damn you android! I'm using a REST service here, stop
// trying to interpret my errors!
x.setMessage("Unable to retrieve message");
}
return x;
}
} catch (Exception x) {
x.printStackTrace();
/*
* if (!retried && x.toString().equals(
* "java.io.IOException: Persistent connection dropped after first chunk sent, cannot retry"
* )) { retry = true; } if (!retry) { return x; }
*/
return x;
} finally {
try {
out.close();
} catch (Exception x) {
}
try {
is.close();
} catch (Exception x) {
}
try {
c.close();
} catch (Exception x) {
}
params.clear();
}
// return null;
}
公共对象服务器连接(字符串url、字符串方法、,
哈希表参数){
httpc=null;
InputStream=null;
OutputStream out=null;
ByteArrayOutputStream postDataByteArrayImage=新建ByteArrayOutputStream();
字节[]数据;
字符串边界=“------------V2ymHFg03ehbqgZCaKO6jy”;
试一试{
如果(!url.startsWith(“/”){
url=“/”+url;
}
字符串uri=Control.URL\u Secure+Control.dtserver+“:”
+Control.port+url;
ByteArrayOutputStream postDataByteArray=新建ByteArrayOutputStream();
参数put(“sessionId”,Control.sessionId);
if(method.equals(“GET”)){
uri=uri+“?”;
枚举enumParams=params.keys();
while(enumParams.hasMoreElements()){
如果(!uri.endsWith(“?”){
uri=uri+“&”;
}
字符串键=(字符串)enumParams.nextElement();
uri=uri
+钥匙
+ "="
+java.net.URLEncoder.encode((字符串)参数
.get(key));
}
}else if(方法等于(“POST”)){
枚举enumParams=params.keys();
postDataByteArray.write((“-”).getBytes();
write((boundry.getBytes());
postDataByteArray.write((“\r\n”).getBytes();
while(enumParams.hasMoreElements()){
字符串键=(字符串)enumParams.nextElement();
如果(!key.equals(“image”)){
后数据字节数组
.write((“内容处置:表单数据;名称=\”)
.getBytes());
write((key.getBytes());
postDataByteArray.write((“\”).getBytes();
postDataByteArray.write((“\r\n\r\n”).getBytes();
postDataByteArray.write(((字符串)params.get(键))
.getBytes());
postDataByteArray.write((“\r\n”).getBytes();
postDataByteArray.write((“-”).getBytes();
write(boundry.getBytes());
postDataByteArray.write((“\r\n”).getBytes();
}
}
postDataByteArray.close();
}
Log.i(“URL”,uri);
URL URLToconent=新URL(uri);
URLConnection=urltoconnect.openConnection();
HttpURLConnection urlConnection=(HttpURLConnection)连接;
URLConnection.setDefaultRequestProperty(“方法”,Method);//默认值
setRequestProperty(“用户代理”、“Android”);
if(方法等于(“POST”)){
urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(postDataByteArray.toByteArray().length+postDataByteArrayImage.toByteArray().length);
urlConnection.setRequestProperty(“内容类型”,
“多部分/表单数据;边界=”+边界);
out=urlConnection.getOutputStream();
write(postDataByteArray.toByteArray());
write(postDataByteArrayImage.toByteArray());
out.close();
}
int响应=0;
试一试{
response=urlConnection.getResponseCode();
}捕获(IOE异常){
if(例如toString()
.equals(“java.io.IOException:收到的身份验证质询为null”))
抛出新的RESTException(401,“无效电话或Pin”);
其他的
投掷e;
}
if(response==HttpURLConnection.HTTP\u OK){
is=urlConnection.getInputStream();
如果(is==null){
返回新IOException(
“无法打开HTTP InputStream,正在中止”);
}
ByteArrayOutputStream bo=新建ByteArrayOutputStream();
int-ch;
整数计数=0;
而((ch=is.read())!=-1){
bo.write(ch);
计数++;
}
data=bo.toByteArray();
返回新字符串(数据);
}否则如果(响应==500){
返回新的RESTException(500,“内部服务器错误”);
}否则{
RESTException x=新的RESTException();
x、 设定码(应答);
试一试{
is=urlConnection.getInputStream();
如果(is==null){
x、 setMessage(“无法检索消息”);
返回x;
}
ByteArrayOutputStream bo=新建ByteArrayOutputStream();
int-ch;
整数计数=0;
而((ch=is.read())!=-1){
urlConnection.setRequestProperty("connection", "close");