从 PHP 文件接收 HTTP POST 回显响应(发送 POSTS 工作正常,这是我无法弄清楚的接收)
Posted
技术标签:
【中文标题】从 PHP 文件接收 HTTP POST 回显响应(发送 POSTS 工作正常,这是我无法弄清楚的接收)【英文标题】:recieving HTTP POST echo response from a PHP file (sending the POSTS works fine, it's the receive that I can't figure out) 【发布时间】:2012-08-11 09:17:57 【问题描述】:所以正如标题所暗示的那样,我的问题是得到对我正在制作的 HTTP POST 的响应。 应该发生的是我发送了一堆变量,php 检查数据库并将结果发回给我(作为页面的回显)。
这里是安卓代码:
public class CheckChallenge extends AsyncTask<String, Void, String>
@Override
protected String doInBackground(String... urls)
String response = "";
try
URL = urls[0];
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("victim",NetPlay.myId));
// need to return these to an array
nameValuePairs.add(new BasicNameValuePair("rival",rivalid));
nameValuePairs.add(new BasicNameValuePair("word","null"));
nameValuePairs.add(new BasicNameValuePair("won","0"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.hanged.comli.com/check-rival.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse execute = httpclient.execute(httppost);
HttpEntity entity = execute.getEntity();
//InputStream is = entity.getContent();
//mText.setText(is.toString());
Log.i("postData", execute.getStatusLine().toString());
//HttpEntity entity = response.getEntity();
catch(Exception e)
Log.e("log_tag", "Error in http connection"+e.toString());
return response;
@Override
protected void onPostExecute(String result)
// CHECK ENTIRE DATABASE FOR MY ID //
// IF MY ID IS THERE THEN THAT MEANS IVE CHALLENGED SOMEONE //
这里是 PHP,我认为只是为了完整起见包含这个是可以的: $connect = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die("无法连接"); mysql_select_db("$mysql_database", $connect)or die("不能选择数据库"); session_start();
$victim = $_POST['victim'];
$rival = $_POST['rival'];
$word = $_POST['word'];
$won = $_POST['won'];
$query = "SELECT rival FROM currentgames";
$result = mysql_query($query);
if (!$result)
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
if (mysql_num_rows($result) == 0)
echo "No rows found, nothing to print so am exiting";
exit;
while ($row = mysql_fetch_assoc($result))
echo $row["rival"];
非常感谢您对此提供的任何帮助,让我了解所有这些 HTTP POST 内容。
【问题讨论】:
【参考方案1】:发送 HTTP 请求并读回 HTTP 响应的示例:
String res = "";
String url = "http://www.domain.com/youscript.php";
URL urlObj = new URL(url);
URLConnection lu = urlObj.openConnection();
// Send data - if you don't need to send data
// ignore this section and just move on to the next one
String data = URLEncoder.encode("yourdata", "UTF-8");
lu.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(lu.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(lu.getInputStream()));
String line = "", res = "";
while ((line = rd.readLine()) != null)
res += line;
wr.flush();
wr.close();
System.out.println(res);
【讨论】:
因此,要将其融入到我已经在做的事情中,我可以在我已经拥有的 InputStream 上使用缓冲阅读器,这样可以吗? @RaidenXL 是的。它在我的应用程序上对我有用。我相信您也可以使用getEntity()
选项,但我没有任何经验,因此无法为您提供帮助。以上是关于从 PHP 文件接收 HTTP POST 回显响应(发送 POSTS 工作正常,这是我无法弄清楚的接收)的主要内容,如果未能解决你的问题,请参考以下文章
从返回前置响应的变量或对象中回显 PHP 中 JSON 字符串的一部分