HTTP邮政从安卓到PHP后台后台、邮政、HTTP、PHP

由网友(稳握江山权)分享简介:我想要做的HTTP邮政从机器人到将要运行PHP脚本来接收这些数据和内容保存到一个文本文件中的后端服务器。I want to do HTTP Post from Android to a backend server that will be running a PHP script to receive those...

我想要做的HTTP邮政从机器人到将要运行PHP脚本来接收这些数据和内容保存到一个文本文件中的后端服务器。

I want to do HTTP Post from Android to a backend server that will be running a PHP script to receive those data and save the contents to a text file.

有关Android的HTTP后,我下面,可以在的 http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

For Android HTTP Post, I am following the example that can be found at http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

不过,我无法找到后端PHP部分的任何引用。最接近的是PHP上传方法 HTTP://www.php .NET /手动/ EN / features.file-upload.post-method.php 但它是文件上传。

But, I couldn't find any reference for the backend PHP portion. The closest is PHP upload methods http://www.php.net/manual/en/features.file-upload.post-method.php but it is for file uploads.

有一个老帖子在SO Get但通过HttpPost在Java中发送到PHP脚本的数据,但没有答案了。

There is an old post in SO Get Data Sent by HttpPost in Java to php Script but no answers yet too.

任何想法如何,我可以实现这个?基本上,我希望收到来自Android上发布的数据和内容保存到一个文本文件中。

Any idea how I could implement this? Basically I want to receive the posted data from Android and save the contents into a text file.

在此先感谢

推荐答案

首先确保你的script.php越来越请求。

First of all make sure your script.php is getting request or not.

测试在仿真器:的本地主机是指在其上code上的设备,如果它的仿真器的话you'l必须使用 IP地址10.0.2.2

Testing On Emulator: The localhost refers to the device on which the code is running, If its about the emulator then you'l have to use IP address 10.0.2.2.

测试在设备上:如果您正在测试您的应用程序的物理设备上,然后you'l必须打开USB绑定,然后检查设备和PC之间关联的IP地址。 (不要紧,如果移动数据是打开还是关闭)

Testing On Device: If you are testing your application on your physical device, then you'l have to turn on USB Tethering and then check the ip address associated between your device and pc. (Doesn't matter if Mobile Data is on or off)

在的Linux ,您可以查看使用该IP地址须藤IPCONFIG

In Linux, you can check that IP address using sudo ipconfig

这将O / P所有网络接口,从这些you'l必须找到一个与 usn0 或一些类似的。

It will o/p all network interfaces, from those you'l have to find the one with usn0 or some similar.

usb0  Link encap:Ethernet  HWaddr 06:94:c7:0d:51:43  
      inet addr:192.168.42.106  Bcast:192.168.42.255  Mask:255.255.255.0
      inet6 addr: fe80::494:c7ff:fe0d:5143/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:21 errors:0 dropped:0 overruns:0 frame:0
      TX packets:29 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:2016 (1.9 KiB)  TX bytes:3978 (3.8 KiB)

在这里,您可以看到设备的IP是 192.168.42.106 那么,在Android中你应该使用URL一样,

Here you can see the device IP is 192.168.42.106 so, In Android you should use URL like,

http://192.168.42.106/script.php

在窗口有类似的命令 IPCONFIG

Android源

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.42.106/script.php"); // <-- for Device, and for emulator--> http://10.0.2.2/script.php 

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
       // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

在的建立正确的IP在Android的code,读取POST参数在的script.php ,并创建一个新的文件来存储数据:

After setting-up correct IP in Android code, read POST arguments in script.php and create a new file to store that data:

<?php
//getting data from POST request.
$arg1= $POST['id'];
$arg2= $POST['stringdata'];

//creating file
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't create file");

//wrinting content to file
fwrite($ourFileHandle, $arg1);
fwrite($ourFileHandle, $arg2);

//closing file
fclose($ourFileHandle);

?>

希望这将是有益的。 :)

Hope this would be useful. :)

阅读全文

相关推荐

最新文章