[英]Get response from request PHP in Android
I have this code in an asynctask in my android proyect to upload a file to server with PHP:我在我的 android proyect 的 asynctask 中有此代码,用于使用 PHP 将文件上传到服务器:
URL url = new URL(args[1]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("uploaded_file", args[0]);
dataOutputStream = new DataOutputStream(connection.getOutputStream());
dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + args[0] + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer,0,bufferSize);
while (bytesRead > 0){
dataOutputStream.write(buffer,0,bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
bytesRead = fileInputStream.read(buffer,0,bufferSize);
}
dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
Log.d(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);
fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();
This is the PHP code in the server side:这是服务器端的PHP代码:
$file_path = "./uploads/";
$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path) ){
echo "success";
} else{
echo "fail";
}
Everything works fine, the file is uploaded correctly but the only response that the server gives me in the code line below:一切正常,文件已正确上传,但服务器在下面的代码行中给我的唯一响应是:
Log.d(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);
It is:这是:
Server Response is: OK: 200服务器响应是:OK:200
Or:或者:
Server Response is: NOT FOUND: 404服务器响应是:未找到:404
I wanna get the strings "success" and "fail" that appears in the server side, How can I do that?我想获得出现在服务器端的字符串“成功”和“失败” ,我该怎么做? Thanks in advance.提前致谢。
Yeah HttpUrlConnection#getResponseMessage
just returns the HTTP Response Message (ie 200 = HTTP OK).是的HttpUrlConnection#getResponseMessage
只是返回 HTTP 响应消息(即 200 = HTTP OK)。 What you want to do is something like:你想要做的是:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// read the contents of the connection from the bufferedreader
.getResponseMessage()
is the HTTP status Message, not the output from the request. .getResponseMessage()
是 HTTP 状态消息,而不是请求的输出。
You need to use a different function, like connection.getContent()
or loop over connection.getInputStream()
您需要使用不同的函数,例如connection.getContent()
或循环connection.getInputStream()
serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
String serverResponseContent = connection.getContent();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.