2016-12-07 53 views
-1

我嘗試將數據放入我的數據庫,但我有例外,如主題。將String轉換爲JSONObject可能是問題,但我不知道如何解決這個問題。Android排除異常:org.json.JSONException:值<br><java.lang.String類型的表不能轉換爲JSONObject

這裏是我的OrderRequest

public OrderRequest(String orderNumber, String date, String adress, int phone, String email, String orderCode, int price, String time, int state, Response.Listener<String> listener){ 
     super(Method.POST, ORDER_REQUEST_URL, listener, null); 
    params = new HashMap<>(); 
    params.put("ordernumber", orderNumber); 
    params.put("data", date); 
    params.put("adress", adress); 
    params.put("phone", phone + ""); 
    params.put("email", email); 
    params.put("ordercode", orderCode); 
    params.put("price", price + ""); 
    params.put("time", time); 
    params.put("state", state + ""); 

} 
@Override 
public Map<String, String> getParams(){ 
    return params; 
} 

這是我的PHP代碼:

<?php 
$con = <-here is password, etc... 


$ordernumber = $_POST["ordernumber"]; 
$date = $_POST["date"]; 
$adress = $_POST["adress"]; 
$phone = $_POST["phone"]; 
$email = $_POST["email"]; 
$ordercode = $_POST["ordercode"]; 
$price = $_POST["price"]; 
$time = $_POST["time"]; 
$state = $_POST["state"]; 


$statement = mysqli_prepare($con, "INSERT INTO order (ordernumber, date, adress, phone, email, ordercode, price, time, state) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
mysqli_stmt_bind_param($statement, "sssissisi", $ordernumber, $date, $adress, $phone, $email, $ordercode, $price, $time, $state); 
mysqli_stmt_execute($statement); 

$response = array(); 
$response["success"] = true; 

echo json_encode($response); 

>

這裏是我的Java代碼:

    Response.Listener<String> responsListener = new Response.Listener<String>() { 
         @Override 
         public void onResponse(String response) { 
          try { 
           JSONObject jsonResponse = new JSONObject(response); 
           boolean success = jsonResponse.getBoolean("success"); 

           if (success) { 
            Intent intent = new Intent(FinalizeActivity.this, LoggedActivity.class); 
            FinalizeActivity.this.startActivity(intent); 
           } else { 

           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        }; 

        OrderRequest orderRequest = new OrderRequest(orderNumberO, dateO, adressO, phoneO, emailO, orderCodeO, priceO, timeO, stateO, responsListener); 
        RequestQueue queue = Volley.newRequestQueue(FinalizeActivity.this); 
        queue.add(orderRequest); 
       } 
      } 
     }); 

在其他放在我的ap我有一個類似的代碼,然後它的工作。

+0

你在某處迴應HTML,這是你的例外告訴你的。因爲HTML不能被解析爲JSON,所以你會得到一個異常 – 0xDEADC0DE

+0

它的'data' not'date'在php代碼 –

+0

中尋找拼寫錯誤,你得到一個HTML內容的響應,並且在這一行中'new JSONObject(response);'它不能轉換它。首先找出你的迴應,然後如果它是json格式,請將它轉換爲Notepad ++中的 –

回答

0

雙方的UTF-8編碼。

    來自編碼標籤並運行此代碼
  • PHP代碼記事本++並選擇編碼以UTF-8無BOM
  • PHP側:報頭( '內容類型=應用/ JSON;字符集= UTF-8');
+0

我選擇utf-8不帶BOM的編碼選項。但是我用這個做了什麼:header('Content-type = application/json; charset = utf-8');我應該把它放在php代碼中? – Akida

+0

如果問題仍然存在是的,你應該放置這個PHP代碼。 –

相關問題