我将事件结果对的HashMap存储为两个字符串,其中第一个字符串可以是任何正整数,它表示id,第二个字符串是可能的结果。可以是“ 1”,“ X”或“ 2”。因此,例如,这将是有效的HashMap:
32
9X
81
12X
我正在尝试使用php检查数据库中所有这些ID,以检查哈希图中的结果是否与该事件的数据库中的结果相同。如果尚未发生该事件,则在数据库中存储为“结果”的结果可以是“ 1”,“ X”或“ 2”或“空”。目前,我正在使用如下所示的for循环,并为每个ID创建一个新的Httpconnection。此方法有效,但是效率极低,如果尝试检查大量数据,则会收到“连接被拒绝,超时”错误。我确定可以通过将ID字符串数组传递给php并检查php中的每个ID并将事件结果对数组返回给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"]
访问。
希望能帮助到你。如果您遇到任何麻烦,请告诉我。
PHP json_encode数组到javascript关联数组 - javascript我有一些从PHP中读取的mysqli列。它正在完美地获取和回显。$results = mysqli_fetch_assoc(mysqli_query($conn, $querystring)); echo json_encode($results); //$results = {"title":"Sea Shells"…
PHP-显示特殊字符 - javascript允许用户输入的代码段//fetch user input and pass it in URL alertify.prompt("Please enter note/remarks for this Form (optional):", function (e,value) { if (e) { alertify.success(…
Mongo汇总 - javascript我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …
验证php中的javascript对象 - php在我的用户界面中,用户可以构建一些javascript对象,例如:var box = { "width": "100px", "height": "200px", "click": function () { alert("You clicked t…
和字符中断通过PHP接收JSON - javascript我有一个包含更多对象的数组。如果一个对象包含&字符,则php不会接收&之后的每个对象。可能是什么问题?这就是阿贾克斯xmlhttp.open("POST", "get.php"); xmlhttp.setRequestHeader("Content-Type", "application/…