【问题标题】:Android Upload Response Message Not Expected不期望 Android 上传响应消息
【发布时间】:2012-08-07 10:14:14
【问题描述】:

这是我的安卓代码:

try
        {
        FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile3) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"profile\";filename=\"" + pathToOurFile3 +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    int serverResponseCode = connection.getResponseCode();
    String serverResponseMessage = connection.getResponseMessage();
    Log.d("serverResponseCode"+"", serverResponseCode +"");
    Log.d("serverResponseMessage", serverResponseMessage);

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
    Log.d("upload", ex.toString());//Exception handling
    }

这里是php代码:

<?php
$target_path  = "./";
$target_path = $target_path . basename( $_FILES['profile']['name']);
if(move_uploaded_file($_FILES['profile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['profile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}
?>

文件可以上传到服务器。但是,响应消息与我预期的不同。
来自 Logcat,serverResponseCode is 200serverResponseMessage is OK
但从 php 中,我想收到类似 " XXX has been upload" 的消息。
有谁知道如何获取消息??

【问题讨论】:

    标签: php android httpconnection


    【解决方案1】:

    我终于得到了答案:

    InputStream is = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String stringResponse = sb.toString();
            Log.d("response string:", stringResponse);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      相关资源
      最近更新 更多