HttpPost не работает! - PullRequest
       2

HttpPost не работает!

1 голос
/ 25 ноября 2010

Я пытаюсь что-то записать в пустой файл на моем сервере 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 и, похоже, делаю все правильно, "я думаю"

Моя проблема ... содержимое файла не обновляется!

Пример был бы великолепен!

Ответы [ 2 ]

4 голосов
/ 25 ноября 2010

Попробуйте это:

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 голосов
/ 10 апреля 2012

Вы можете вызвать 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...