在安卓中发布到 PHP

本文关键字:PHP | 更新日期: 2023-09-27 18:30:34

我写了一个向php页面发送信息的Windows软件使用以下代码 (C#):

string URL = "http://mydomin.com/page.php";
WebClient webClient = new WebClient();
NameValueCollection formData = new NameValueCollection();
formData["INSERT"] = "DB";
formData["Title"] = subTxt.Text;
formData["Content"] = contentTxt.Text;
byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
string responsefromserver = Encoding.UTF8.GetString(responseBytes);
webClient.Dispose();

在 PHP 中:

if($_POST['INSERT'] != '')
{
    $subSended = $_POST['Title'];
    $contentSended = $_POST['Content'];
    ...
    ...
    ...
    echo "OK";
}

请帮助我在Android中实现此代码。

对不起我的英语。

在安卓中发布到 PHP

像这样尝试

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mydomin.com/page.php");
try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("INSERT", "DB"));
    nameValuePairs.add(new BasicNameValuePair("Title", "YOUR TITLE"));
    nameValuePairs.add(new BasicNameValuePair("Content", "YOUR TITLE"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
相关文章:
  • 没有找到相关文章