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

时间:2015-03-13 20:44:59

标签: java php android json file-upload

我遵循本教程。

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

如果您按照教程操作,我已将fileUpload.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):链接:{"错误":false,"消息":"文件上传成功! "" FILE_PATH":" HTTP://68.169.50.115/AndroidFileUpload/IMG_20150313_164010.jpg"}

它表示图片存在,但如果您尝试链接..

http://68.169.50.115/AndroidFileUpload/IMG_20150313_164010.jpg

它没有用。

它是否工作不正常,因为我的php文件保存在我的云端服务器而不是我的个人电脑上,就像教程所指示的那样?

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

这里是文件upload.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);
?>

1 个答案:

答案 0 :(得分:1)

我能够让这个装饰工作。让它与Android Studio一起工作有点挑战。

我遇到的最后一个问题是“uploads”文件夹的文件夹权限需要设置为Group和Public的Write权限。 所以,我猜这可能是你遇到的问题。

我还使用了下面修改过的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';
}