【问题标题】:Send information from PHP to Java将信息从 PHP 发送到 Java
【发布时间】:2010-05-25 11:53:46
【问题描述】:

我想将一些信息从 PHP 发送到 Java。为什么?因为我的服务器上有一个数据库,并且我使用 PHP 脚本从我的数据库中获取信息。

现在我想用 Java 将该信息发送给我的客户。我该怎么做?

我通过 POST 从 Java 向 PHP 发送信息,它运行良好,但我不知道如何进行反向操作。

你能帮帮我吗?

我从 Java 的 GET 连接中看到了这段代码……它正确吗?

public String HttpClientTutorial(){
    String url = "http://testes.neoscopio.com/myrepo/send.php";
       InputStream content = null;  
       try {  
         HttpClient httpclient = new DefaultHttpClient();  
         HttpResponse response = httpclient.execute(new HttpGet(url)); 

         content = response.getEntity().getContent();  
       } catch (Exception e) {  
         Log.e("[GET REQUEST]", "Network exception", e);  
       }  


  String response = content.toString();

  return response;
}

P.S:我是安卓开发者,不是Java开发者...

【问题讨论】:

  • 安卓开发者是什么意思??
  • 那么您是说您希望 Java 部分充当 PHP 连接到的服务器,还是您的意思是您希望响应 Java 客户端发送给您 PHP 的 POST 调用?

标签: java php android


【解决方案1】:

来自exampledepot: Sending POST request(已修改以获取send.php 的输出。)

try {
    // Construct data
    String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");

    // Send data
    URL url = new URL("http://testes.neoscopio.com/myrepo/send.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        // Process line...
    }
    wr.close();
    rd.close();
} catch (Exception e) {
}

附:这在 Android 上应该可以正常工作。 :)

(我通常import static java.net.URLEncoder.encode,但这只是个人喜好问题。)

【讨论】:

  • exampledepot 的代码显然没有使用testes.neoscopio.com,这就是我改变的:-)
  • 当然.. 但是在文件 send.php 我如何将信息发送到 java?这段代码会对 php 有所帮助吗?
  • 我还有 1 个问题.. 可以通过“GET”连接发送信息吗? like.. 发送一个字符串到 send.php.. 并接收相同的输出..
  • 当然。将网址更改为send.php?yourVariable=aValue
猜你喜欢
  • 2012-03-21
  • 2013-02-25
  • 2014-11-16
  • 2012-08-31
  • 1970-01-01
  • 2023-03-04
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多