gpt4 book ai didi

php - 从 Android 到 PHP 的 POST 后自定义 HttpResponse

转载 作者:行者123 更新时间:2023-11-29 21:51:47 24 4
gpt4 key购买 nike

我有一个对 php 页面执行 POST 的类,该页面应该对发布的数据进行一些操作,并返回结果(类似于 OK,KO)。如何用 PHP 发送结果,以及如何在 android 中使用它?

在 Android 方面我有这个,它发布了数据:

public int postData(String id, String webrispid, String userid) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.server.com/postpage.php");
int responseCode = 0;
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", id));
nameValuePairs.add(new BasicNameValuePair("user", userid));
nameValuePairs.add(new BasicNameValuePair("webrisid", webrispid));
try {
nameValuePairs.add(new BasicNameValuePair("token",AeSimpleSHA1.SHA1(userid) ));
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseCode = response.getStatusLine().getStatusCode();
System.out.println(response);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return responseCode;
}

实际上 postpage.php 看起来像这样:

<?php

if ( isset($_POST['user']) ) {
// try to store the data posted

//if data is stored return "ok"

// else return "ko"

} else {
// error
}
?>

所以在 android 端,我应该停止发送相同的数据并开始处理其余数据并最终将其发送到页面...

最佳答案

如果只是“OK”或“KO”,您可以执行以下操作:

<?php
if ( isset($_POST['user']) ) { // try to store the data posted
//do some test
echo "OK"; //or KO
} else {
echo "ERROR";
}
?>

android 部分看起来像这样:

// Execute HTTP Post Request
String status = httpclient.execute(httpPost, new BasicResponseHandler());
System.out.println(status); //will be OK or KO or ERROR

关于php - 从 Android 到 PHP 的 POST 后自定义 HttpResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14213150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com