【问题标题】:HttpPost not working!HttpPost 不工作!
【发布时间】:2010-11-25 01:03:29
【问题描述】:

我正在尝试向我的 apache 服务器上的一个空文件写入一些内容

myClient= new DefaultHttpClient();

StringEntity myString = new StringEntity("important message");
HttpPost httpPost = new HttpPost("http://10.0.218.211/myfile");
httpPost.setEntity(myString);

HttpResponse response = myClient.execute(httpPost);

响应返回“HTTP/1.1 200 OK”,因此它确实找到了文件

我尝试删除文件并返回错误 404

我阅读了 apache 的文档,看起来“我认为”做得对

我的问题是……文件的内容没有更新!

举个例子就好了!

【问题讨论】:

  • 您使用什么编程语言和框架编写 Web 应用程序?您是否使用其他客户端(例如 curl)测试了 Web 应用程序?换句话说,为什么这是一个 Android 问题,而不是 Web 服务器或 Web 应用程序问题?
  • 我必须同意 CommonsWare,这里的问题很可能是您在收到 post 请求时在服务器端所做的事情。
  • 服务器是 Ubuntu 10.10 上的 apache2,我可以从我的浏览器访问它。我也可以从服务器读取和解析 XML 文件,但是我不知道如何写入服务器上的 XML 文件。我的问题中的“myfile”是实际的 XML 文件,这是它的本意还是应该像 php 脚本或其他东西来处理 http 帖子?我和他们在 android 上一样菜鸟,但我对任何不便表示歉意

标签: android http http-post


【解决方案1】:

试试这个:

url = "http://10.0.218.211/post.php"; 

HttpClient httpclient = new DefaultHttpClient();  
HttpPost post = new HttpPost(url);  

try {  
        **// Add your data <-**
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
        nameValuePairs.add(new BasicNameValuePair("message", "important message 1"));  
        nameValuePairs.add(new BasicNameValuePair("message2", "important message 2"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));  

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

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

AndroidManifest.xml

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

post.php

<?php

$message = $_POST['message'];

// Load XML file
$xml = simplexml_load_file("myfile.xml"); 

 //In this line it create a SimpleXMLElement object with the source of the XML file.
$sxe = new SimpleXMLElement($xml->asXML());

//The following lines will add a new child and others child inside the previous child created.
$item = $sxe->addChild("item");
$item->addChild("message", $message);

//This next line will overwrite the original XML file with new data added
$sxe->asXML("myfile.xml"); 

?>

myfile.xml

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <item>
        <message>Important Message</messagee>
    </item>
</data>

【讨论】:

  • 感谢您的快速回答,我的 xml 文件上仍然没有出现任何内容,并且响应仍然是 HTTP/1.1 200 OK,“myfile”是实际的 xml 文件,我应该像 php 那样发布不同的内容吗?
  • 你的兄弟,但我仍然从响应中得到 HTTP/1.1 200 OK 并且文件仍然是空的。我创建了 post.php 并复制了 php 和 android 代码但仍然没有用:(
  • 我会试试你放的新 php 脚本
  • 它现在给我 500 内部服务器错误
  • 我的服务器上安装了 php-5 的 apache2
【解决方案2】:

您可能想要调用 setcontentType。 例如

StringEntity myString = new StringEntity("important message");
myString.setContentType("application/json");//or what ever your server expected
HttpPost httpPost = new HttpPost("http://10.0.218.211/myfile");
httpPost.setEntity(myString);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多