2016-09-13 33 views
0

我有這樣的代碼在我的Android proyect一個的AsyncTask將文件上傳到服務器與PHP:獲得來自請求PHP響應Android中

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(); 

這是在服務器端的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"; 
} 

一切工作正常,該文件被上載正確,但服務器唯一的反應給我的代碼線以下:

Log.d(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode); 

是:

服務器響應是:OK:200

或者:

服務器的迴應是:未找到:404

我想要得到的字符串「成功」「失敗」出現在服務器端,我該怎麼做?提前致謝。

回答

0

HttpUrlConnection#getResponseMessage只是返回HTTP響應消息(即200 = HTTP OK)使用不同的功能,如connection.getContent()或循環。你想要做的是這樣的:

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 


// read the contents of the connection from the bufferedreader 
+1

我做了這個BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder total = new StringBuilder(); 絃線; ((line = in.readLine())!= null){ total.append(line).append('\ n'); (TAG,「服務器響應是:」+ total.toString()+「:」+ serverResponseCode);'和完美的工作,謝謝! – Jorius

0

.getResponseMessage()是HTTP狀態消息,而不是請求的輸出。

需要超過connection.getInputStream()

serverResponseCode = connection.getResponseCode(); 
String serverResponseMessage = connection.getResponseMessage(); 
String serverResponseContent = connection.getContent();