简体   繁体   English

从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)

So as the title suggest my problem is getting a response to a HTTP POST I'm making. 因此,正如标题所示,我的问题是得到我正在发出的HTTP POST的响应。 What SHOULD be happening is I send a bunch of variables, the PHP checks the database for them and sends back to me the result (as an echo to the page). 应该发生的是,我发送了一堆变量,PHP检查数据库中的变量并将结果发送回给我(作为对页面的回显)。

Here is the android code: 这是android代码:

 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 //


    }

  }

Here is the PHP which I think is ok just including this for completeness: $connect = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password")or die("cannot connect"); 这是我认为可以的PHP,仅出于完整性考虑就可以了:$ connect = mysql_connect(“ $ mysql_host”,“ $ mysql_user”,“ $ mysql_password”)或die(“ cannot connect”); mysql_select_db("$mysql_database", $connect)or die("cannot select DB"); mysql_select_db(“ $ mysql_database”,$ connect)或die(“无法选择数据库”); session_start(); 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"];
}

Any help with this would be very appreciated, trying to get my head around all this HTTP POSTing stuff. 对此的任何帮助将不胜感激,尝试使我了解所有这些HTTP POSTing内容。

Example of sending an HTTP request and reading back the HTTP response: 发送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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM