【问题标题】:Connect Android Device (PhoneGap) in Eclipse to 127.0.0.1将 Eclipse 中的 Android 设备 (PhoneGap) 连接到 127.0.0.1
【发布时间】:2013-09-13 10:13:00
【问题描述】:

我正在使用 Eclipse 和 PhoneGap 来开发应用程序。对于运行配置,我使用的是 Android 设备,因为它比使用 AVD 更快。

所以,我想做的是将手机中的文件(通过笔记本电脑连接到 Eclipse 的 USB)上传到我的本地主机服务器。主要是把文件上传到远程服务器。但至于最开始的部分,我只想上传到本地主机。

(index.html) 这是我从 HTML 上传的脚本:

<!DOCTYPE html>
<html>
  <head>
    <title>Capture Photo</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    var pictureSource;   // picture source
    var destinationType; // sets the format of returned value

    document.addEventListener("deviceready",onDeviceReady,false);

    function onDeviceReady() {
        pictureSource=navigator.camera.PictureSourceType;
        destinationType=navigator.camera.DestinationType;
    }

    function onPhotoDataSuccess(imageData) {
      var smallImage = document.getElementById('smallImage');

      smallImage.style.display = 'block';

      smallImage.src = "data:image/jpeg;base64," + imageData;

      var options = new FileUploadOptions();
      options.fileKey="file";
      options.fileName=imageData.substr(imageData.lastIndexOf('/')+1);      
      options.mimeType="image/jpeg";
      console.log("Filename: " + options.fileName);
      var params = new Object();
      params.value1 = "test";
      params.value2 = "param";
      options.params = params;
      options.chunkedMode = false;
      var ft = new FileTransfer();
      ft.upload(imageData, "http://127.0.0.1:80/upload_file.php", win, fail, options);
    }

    function capturePhoto() {
      navigator.camera.getPicture(onPhotoDataSuccess, onFail,
        { 
          quality: 50,
          sourceType : Camera.PictureSourceType.CAMERA,
          destinationType: destinationType.DATA_URL
        }
      );
    }

    function win(r) {
      console.log("Code = " + r.responseCode);
      console.log("Response = " + r.response);
      console.log("Sent = " + r.bytesSent);
      alert(r.response);
    }

    function fail(error) {
      alert("An error has occurred: Code = " = error.code);
    }

    function onFail(message) {
      alert('Failed because: ' + message);
    }

    </script>
  </head>
  <body>
    <button onclick="capturePhoto();">Capture Photo</button> <br>
    <img style="display:none;width:300px" id="smallImage" src="" />
    <img style="display:none;" id="largeImage" src="" />

  </body>
</html>

是的,代码的作用是用相机拍摄照片,然后将照片上传到服务器。但我这里主要关注的是上传它。

(c:/wamp/www/upload.php) 这是服务器端的 php 脚本:

 <?php
if ($_FILES["file"]["error"] > 0) {
  echo "Return Code: " . $_FILES["file"]["error"] . "";
} else {
  echo "Upload: " . $_FILES["file"]["name"] . "";
  echo "Type: " . $_FILES["file"]["type"] . "";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "";
  if (file_exists("D:/PHPUPLOAD/" . $_FILES["file"]["name"])) {
    echo $_FILES["file"]["name"] . " already exists. ";
  } else {
    move_uploaded_file($_FILES["file"]["tmp_name"], "C:/PHPUPLOAD/" . $_FILES["file"]["name"]); //Save location
    echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}
?>

如果我使用 Android 设备而不是 AVD 应该没问题吧?

【问题讨论】:

    标签: javascript html cordova wamp


    【解决方案1】:

    如果您在同一台机器上,您可以使用 127.0.0.1 访问模拟器的主机。如果要到达开发机,可以使用10.0.2.2。

    参考How to get the Android Emulator's IP address?

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      Usb 连接不提供对移动设备的网络访问。

      要连接到 localhost,请参见:

      How can I access my localhost from my Android device?

      如果您希望在远程服务器上运行相同的代码,那么 phonegap 可以选择域白名单。

      你可以在这里获得更多信息:

      http://docs.phonegap.com/en/edge/guide_appdev_whitelist_index.md.html#Domain%20Whitelist%20Guide

      【讨论】:

        【解决方案3】:

        我终于找到了解决办法。我忘了提到我的设备和笔记本电脑在同一个网络(wifi)内,所以为了让它连接到笔记本电脑的 localhost,我需要找到它的本地 IP 地址。

        cmd 中输入ipconfig 并找到主机的IPv4 地址。例如:

        Wireless LAN adapter Wireless Network Connection:
        
           Connection-specific DNS Suffix  . :
           Link-local IPv6 Address . . . . . : fe80::f0b2:6e7f:53fb:24e8%13
           IPv4 Address. . . . . . . . . . . : 192.168.1.3
           Subnet Mask . . . . . . . . . . . : 255.255.255.0
           Default Gateway . . . . . . . . . : 192.168.1.1
        

        就我而言,它是192.168.1.3

        要使其可见,您需要允许 Apache 服务器接受所有传入的请求。为此,请打开 Apache 的 httpd.conf 文件并修改以下内容:

        <Directory "c:/wamp/www">
            Options Indexes FollowSymLinks Includes ExecCGI
            AllowOverride All
            Order Deny,Allow
            Allow from all
        </Directory>
        

        测试您手机中的 IP 地址。您应该会看到 WAMP 主页。

        使用index.html 中的这一行连接到您的本地主机

        ft.upload(imageData, "http://192.168.1.3/upload_file.php", win, fail, options);
        

        对于upload_file.php

        <?php
            print_r($_FILES);
            $new_image_name = "namethisimage.jpg";
            move_uploaded_file($_FILES["file"]["tmp_name"], $new_image_name);
        
            echo "<img width='500px' src='namethisimage.jpg'>";
        ?>
        

        谢谢大家的回复:)

        【讨论】:

        • 嗨,这对我来说是正确的代码 android 代码中的代码是什么?本地主机上的多部分图像上传
        猜你喜欢
        • 1970-01-01
        • 2012-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多