【问题标题】:Sending data to a php server [closed]将数据发送到 php 服务器 [关闭]
【发布时间】:2014-04-27 16:20:23
【问题描述】:

我想做的是将数据发送到 php 服务器但不访问该站点 或使用浏览器。
通常在浏览器中是这样的:

/test/index.php?name1=value1&name2=value2

我想使用 Android(Java) 将 name1=value1&name2=value2 发送到服务器。

POST 或 GET 都适合我。

【问题讨论】:

  • 这里真的有问题吗?

标签: java php android


【解决方案1】:

您可以通过以下方式实现:

final HttpGet uri = new HttpGet("http://example.com/test/index.php?name1=value1");
final DefaultHttpClient httpClient = new DefaultHttpClient();
final HttpResponse response = httpClient.execute(uri);

不要忘记将<uses-permission android:name="android.permission.INTERNET" /> 添加到您的清单中。

【讨论】:

    【解决方案2】:

    看看 HttpRequest: http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android

    在教程中:key1 和 value1 的作用相同:$_POST['key1'] == "value1"。

    您还需要使用 AsyncTask,因为您无法在 UI 线程上运行网络任务。

    【讨论】:

      【解决方案3】:

      最简单的方法是使用HttpURLConnection(有关更多信息,请参阅this article)。

      import java.net.*;
      import java.io.*;
      
      public class URLConnectionReader {
          public static void main(String[] args) throws Exception {
              URL url = new URL("/test/index.php?key="+ + URLEncoder.encode(value);
      
              URLConnection connection = url.openConnection();
              BufferedReader in = new BufferedReader(new InputStreamReader(
                                          connection.getInputStream()));
              String inputLine;
              while ((inputLine = in.readLine()) != null) 
                  System.out.println(inputLine);
              in.close();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-07-11
        • 2019-02-23
        • 1970-01-01
        • 2012-07-19
        • 2011-07-28
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 2013-07-18
        相关资源
        最近更新 更多