如何使用 Java 解析 php 消息 - HTTP 格式不正确的问题


How to parse a php message with Java - incorrect HTTP format issue

我正在学习本教程。

http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/

如果您按照教程进行操作,我已经将文件上传.php保存到我的服务器。

http://68.169.50.115/AndroidFileUpload/fileUpload.php

这是信息。当您"成功"将图片保存到服务器时,我收到了 JSON 格式。问题是链接不起作用,文件图片也没有实际保存到我的服务器。

    private void showAlert(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(message).setTitle("Response from Servers")
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // do nothing
                    }
                });
        Log.d("OGG", "JSON: " + message);
        AlertDialog alert = builder.create();
        alert.show();
    }
}

这是来自 Log.d 的响应,即由图片形成的 JSON 消息。

03-13 16:40:24.725: D/OGG(5158): 链接: {"错误":假,"消息":"文件上传成功!","file_path":"http://68.169.50.115/AndroidFileUpload/IMG_20150313_164010.jpg"}

它说图片存在,但如果你尝试链接..

http://68.169.50.115/AndroidFileUpload/IMG_20150313_164010.jpg

它不起作用。

它是否无法正常工作,因为我将 php 文件保存在我的云服务器上,而不是像教程指示的那样保存在我的个人计算机上?

为什么文件实际上没有保存。

这是文件上传.php

<?php

// Path to move uploaded files
$target_path = "uploads/";
// array for final json respone
$response = array();
// getting server ip address
$server_ip = gethostbyname(gethostname());
// final file url that is being uploaded
//$file_upload_url = 'http://' . $server_ip . '/';
$file_upload_url = 'http://' . $server_ip . '/' . 'AndroidFileUpload' . '/';

if (isset($_FILES['image']['name'])) {
$target_path = $target_path . basename($_FILES['image']['name']);

// reading other post parameters
$email = isset($_POST['email']) ? $_POST['email'] : '';
$website = isset($_POST['website']) ? $_POST['website'] : '';

 try {
 // Throws exception incase file is not being moved

 if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
 // make error flag true
 $response['error'] = true;
 $response['message'] = 'Could not move the file!';
}

 // File successfully uploaded
 $response['message'] = 'File uploaded successfully!';
 $response['error'] = false;
 $response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
 } catch (Exception $e) {
 // Exception occurred. Make error flag true
 $response['error'] = true;
   $response['message'] = $e->getMessage();
 }
} else {
 // File parameter is missing
$response['error'] = true;
 $response['message'] = 'Not received any file!F';
}
// Echo final json response to client
echo json_encode($response);
?>

我能够让 turorial 工作。 让它与Android Studio一起工作有点挑战。

我遇到的最后一个问题是,"上传"文件夹的文件夹权限需要设置为组和公共的写入权限。所以,我想这可能也是你遇到的问题。

我还使用了下面修改后的 PHP 代码。

如果 move_uploaded_file() 调用失败但不抛出异常,PHP 代码将返回成功响应。 有人在教程中也对此进行了评论。

试试这个,看看你是否仍然得到成功响应: 将成功部分移动到 else 块,这样就不会在失败案例中调用它:

 try {
 // Throws exception incase file is not being moved
  if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
   // make error flag true
   $response['error'] = true;
   $response['message'] = 'Could not move the file!';
  } else {
    // File successfully uploaded
    $response['message'] = 'File uploaded successfully!';
    $response['error'] = false;
    $response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
  }
 } catch (Exception $e) {
   // Exception occurred. Make error flag true
   $response['error'] = true;
   $response['message'] = $e->getMessage();
 }
} else {
 // File parameter is missing
 $response['error'] = true;
 $response['message'] = 'Not received any file!F';
}