我有一个事件-结果对的HashMap,存储为两个字符串,其中第一个字符串可以是任何正整数,这表示id,第二个字符串是可能的结果。它可以是'1' 'X'或'2'。例如这是一个有效的HashMap:
32
9X
81
12X
我试图检查每一个这些Id与我的数据库使用php来检查是否在哈希图的结果是相同的结果在数据库中为该事件。数据库中存储为'Result'的结果可以是'1' 'X'或'2'或'null'(如果事件尚未发生)。目前,我正在使用如下所示的for循环,并为每个ID创建一个新的Httpconnection。这是有效的,但它是令人难以置信的效率低下,我收到"连接拒绝,超时"错误,如果我试图检查大量的数据。我相信可以通过ID字符串的数组传递给php和检查php并返回数组中的每个ID event-outcome对android应用。然而,尽管我找到方法如何通过php数组,我不知道如何将数组的可变长度的HashMap可能在每种情况下不同的长度,和我不知道如何在php中处理任意长度的数组本身。下面是我的java类和两个php文件。提前感谢你的帮助。:)
CheckBet.java
public String checkbetoutcome() {
for (String x : bet.keySet()) {
Log.d("X", x);
currentitem = x;
new LoadAllGamet().execute();
}
for (String x : statuses) {
Log.d("testaaaaa",x);
if (x.equals("open")) {
finalstatus = "open";
}
if (x.equals("lost")) {
finalstatus = "lost";
break;
}
}
return finalstatus;
}
class LoadAllGamet extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(String... args) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url_check_bet);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 2000000);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", currentitem));
Log.d("CURRENTITEM",currentitem);
try {
post.setEntity(new UrlEncodedFormEntity(params));
} catch (IOException ioe) {
ioe.printStackTrace();
}
try {
HttpResponse response = client.execute(post);
Log.d("Http Post Responsecxxx:", response.toString());
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
JSONObject jObj = null;
String json = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("<", 0)) {
if (!line.startsWith("(", 0)) {
sb.append(line + "'n");
}
}
}
is.close();
json = sb.toString();
json = json.substring(json.indexOf('{'));
Log.d("sbsssssssssss", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
game = jObj.toString().substring((jObj.toString().indexOf(':')+1),(jObj.toString().length()-1));
Log.d("GAME",game);
Log.d("jsonsssssssssss", jObj.toString());
if (game.contains("null")) {
String asa = game.substring(game.indexOf("'"") + 1, game.length());
game = asa;
}
else {
String asa = game.substring(game.indexOf("'"")+1,game.length()-1);
game = asa;
}
Log.d("CURRENTITEMSTATUS",game);
Log.d("CURRENTITEAMREAL", bet.get(currentitem));
if (game.equals("null")) {
status = "open";
}
else if (game.equals(bet.get(currentitem))) {
status = "won";
}
else {
status = "lost";
}
Log.d("Status", status);
statuses.add(status);
status = "open";
} catch (IOException ioe) {
ioe.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String param) {
}
// CHANGE THIS AT THE END
}
}
Check_Bets_Handler.php
<?php
if (isset($_POST['param'])) {
// get tag
$array = array();
$array= $_POST['param'];
// include db handler
require_once 'include/Check_Bets.php';
$db = new Check_Bets();
foreach($array as $id) {
// response Array
$result = $db->checkuserbets($id);
$response["bet"] = array();
array_push($response["bet"], $result);
}
echo json_encode($response);
}
else {
$response["error"] = TRUE;
$response["error_msg"] = "the response is null!";
echo json_encode($response);
}
?>
Check_Bets.php
<?php
class Check_Bets {
function __construct() {
require_once 'DB_Connect.php';
$this->db = new DB_Connect();
$this->db->connect();
}
function __destruct() {
}
public function checkuserbets($id) {
$conn=mysqli_connect("****", "****", "**","******");
$result = mysqli_query($conn,"SELECT Result FROM gamelist WHERE gid = '$id'");
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
return mysqli_fetch_array($result,MYSQLI_ASSOC);
}
}
}
?>
我不能100%确定这是否解决了您的目的,但我已经传递了一个不确定长度的数组,以及使用简单技术的关联数组到php。
假设param
是哈希映射的键。您可以在PHP代码中作为param [ index ]
访问它以获得value
。
这里有一个片段给你:
//Assuming you've created a HashMap mHashMap
for (String id : mHashMap.keySet()) {
nameValuePair.add(new BasicNameValuePair("param" + "[" + id + "]" , mHashMap.get(id)));
您可以在PHP脚本中检索这个关联数组作为$_POST["param"]
。将其存储在变量array
中,并使用array["id"]
进行访问。
希望有帮助。