gpt4 book ai didi

java - 从 PHP 文件接收 HTTP POST 回显响应(发送 POSTS 工作正常,这是我无法弄清楚的接收)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:32 26 4
gpt4 key购买 nike

正如标题所暗示的那样,我的问题是获得对我正在制作的 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 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);

关于java - 从 PHP 文件接收 HTTP POST 回显响应(发送 POSTS 工作正常,这是我无法弄清楚的接收),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963190/

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