HttpPost不工作​​! [英] HttpPost not working!

查看:115
本文介绍了HttpPost不工作​​!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写点东西到一个空文件我的Apache服务器

I am trying to write something to an empty file on my apache server

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,所以它确实找到文件

The reponse returns "HTTP/1.1 200 OK" so it does find the file

我尝试删除该文件,它返回错误404

I tried removing the file and it returned Error 404

我看了apache的文档,看起来像我这样做是正确的:我认为

I read apache's docs and looks like am doing it right "i think"

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

My problem is... the contents of the file do not get updated!

一个例子是伟大的!

推荐答案

试试这个:

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>

这篇关于HttpPost不工作​​!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆