如何使用Java解析php消息-错误的HTTP格式问题 - java

我正在关注本教程。

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的响应,Log.d是图片形成的JSON消息。

03月13日16:40:24.725:D / OGG(5158):链接:{“错误”:否,“消息”:“文件已成功上传!”,“文件路径”:“ 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);
?>

参考方案

我得以花钱去上班。使它与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';
}

Java-如何将此字符串转换为日期? - java

我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…

在Java中用'\\'替换单个'\' - java

如何用'\'替换单个'\\'?当我运行replaceAll()时,我收到此错误消息。Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ …

用正斜杠替换反斜杠 - java

我有以下路径:com/teama/merc/test/resTest我想将其转换为:com\teama\merc\test\resTest我正在尝试将上述路径附加到此路径:C:\Users\Toby\git\MERCury\MERCury\使用str.replace('/', '\\');,但是当我将两个字符串都附加到…

错误:任务':app:lintVitalRelease'的执行失败,任何人都可以解决吗? - java

为什么我收到此错误,我尝试清理和重建应用程序并制作应用程序发布为真,并且出现相同的错误 错误:任务':app:lintVitalRelease'的执行失败。 java.lang.IllegalStateException:预期为BEGIN_ARRAY,但位于第1行第1列路径$ apply plugin: 'com.android.applicati…

为什么`if(guess!='a'|| guess!='A'||…)`不起作用? - java

Improve this question 这是我的代码,我知道if语句真的很长,代码可能会更高效,但是我只是想知道答案,因为它使我发疯。while (whileloop == 1) { if (guess != 'a' || guess != 'A' || guess != 'b' || gues…