2010-11-25 118 views
1

我想寫的東西到一個空文件我的Apache服務器上HttpPost不工作​​!

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的文檔,看起來像我這樣做是正確的:「我認爲」

我的問題是...該文件的內容沒有更新!

一個例子會很棒!

+0

您編寫Web應用程序時使用了哪種編程語言和框架?您是否使用其他客戶端測試了Web應用程序,比如`curl`?換句話說,爲什麼這是一個Android問題,而不是Web服務器或Web應用程序問題? – CommonsWare 2010-11-25 01:29:18

+0

我不得不同意CommonsWare,這裏的問題很可能出現在您接收到發佈請求時服務器端的內容中。 – blindstuff 2010-11-25 01:32:56

回答

4

試試這個:

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> 
0

您可能想要調用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);