2015-07-19 2 views

ответ

0

Yes.Check этот пример: How To Post Data From An Android App To a Website

MainActivity: Боковые коды

public class HelloWorldActivity extends Activity { 
    Button sendButton;  
    EditText msgTextField; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     // load the layout 
     setContentView(R.layout.main);   

     // make message text field object 
     msgTextField = (EditText) findViewById(R.id.msgTextField); 
     // make send button object 
     sendButton = (Button) findViewById(R.id.sendButton); 

    } 

    // this is the function that gets called when you click the button 
    public void send(View v) 
    { 
     // get the message from the message text box 
     String msg = msgTextField.getText().toString(); 

     // make sure the fields are not empty 
     if (msg.length()>0) 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://yourwebsite.com/yourPhpScript.php"); 
     try { 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
      nameValuePairs.add(new BasicNameValuePair("message", msg)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      httpclient.execute(httppost); 
      msgTextField.setText(""); // clear text box 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     } 
     else 
     { 
      // display message if text fields are empty 
      Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show(); 
     } 

    } 

} 

Сервер:

<?php 
// get the "message" variable from the post request 
// this is the data coming from the Android app 
$message=$_POST["message"]; 
// specify the file where we will save the contents of the variable message 
$filename="androidmessages.html"; 
// write (append) the data to the file 
file_put_contents($filename,$message."<br />",FILE_APPEND); 
// load the contents of the file to a variable 
$androidmessages=file_get_contents($filename); 
// display the contents of the variable (which has the contents of the file) 
echo $androidmessages; 
?> 
+0

Спасибо за ответ, просто хотел бы знать, будет ли это работать для любого сайта? Я имею в виду, если я не связан напрямую с сервером. – HunterrJ

+0

Я не уверен, что работаю на любом сайте, потому что он использует код 'PHP', и я не имею никакого представления о' Php'. Но это пример использования и отправки с помощью 'POST'. И вы должны его протестировать и узнать php для того, как это работает. – Mohsen

+0

Также некоторые интерфейсы устарели. – HunterrJ

Смежные вопросы