[英]recieving HTTP POST echo response from a PHP file (sending the POSTS works fine, it's the receive that I can't figure out)
因此,正如標題所示,我的問題是得到我正在發出的HTTP POST的響應。 應該發生的是,我發送了一堆變量,PHP檢查數據庫中的變量並將結果發送回給我(作為對頁面的回顯)。
這是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 //
}
}
這是我認為可以的PHP,僅出於完整性考慮就可以了:$ connect = mysql_connect(“ $ mysql_host”,“ $ mysql_user”,“ $ mysql_password”)或die(“ cannot connect”); mysql_select_db(“ $ mysql_database”,$ connect)或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 POSTing內容。
發送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.