我正在开发一个应用程序,它将存储到MySQL
db并从中读取数据。目前我能够成功获取1 row
但是当我尝试抓取multiple rows
时,它无法正常工作。当我尝试在JSONObject
中初始化error
(我在ResponseListener
的行上方添加注释时),代码会中断。要与我的数据库通信,我正在使用排球库。
以下是错误输出:
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:97)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.example.www.myapp.DataActivity$2.onResponse(DataActivity.java:235)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.example.www.myapp.DataActivity$2.onResponse(DataActivity.java:229)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at android.os.Looper.loop(Looper.java:158)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7229)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at java.lang.reflect.Method.invoke(Native Method)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
08-23 17:27:12.853 22689-22689/com.example.www.myapp W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
到目前为止,这是我的代码:
响应侦听器
private void getDataFromExternalDB(final String startid){
id = Integer.parseInt(startid);
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//I get the error at the following line
JSONObject jsonResponse = new JSONObject(response);
while (jsonResponse.getJSONObject(""+id) != null){
jsonResponse.getJSONObject(""+id);
int user_id = jsonResponse.getInt("user_id");
String username = jsonResponse.getString("username");
Log.d("getDataFromExternalDB","user_id="+user_id + " username="+username);
id++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
DataRequest dataRequest = new DataRequest (startid, responseListener);
RequestQueue queue = Volley.newRequestQueue(DataActivity.this);
queue.add(dataRequest );
}
DataRequest.java
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class DataRequest extends StringRequest {
private static final String DATA_REQUEST_URL = "http://192.168.178.17/getData.php";
private Map<String, String> params;
public DataRequest (String startid, Response.Listener<String> listener) {
super(Method.POST, DATA_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("startid", startid);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
访问getdata.php
<?php
$connect = mysqli_connect("localhost", "root", "pass", "db");
$startid = $_POST["startid"];
function getData() {
global $connect, $startid;
$result = $connect->query("SELECT * FROM users WHERE user_id > " . $startid );
$responses = array();
$response = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$response["user_id"] = $row["user_id"];
$response["username"] = $row["username"];
$responses["" . $row["user_id"]]=$response;
}
echo json_encode($responses);
}else {
echo "0 results";
}
}
getData();
$connect->close();
?>
答案 0 :(得分:0)
请回复
尝试{
FLOAT_MAX