Java 分析mysql数据库中的数据时出错
我正在开发一个android应用程序。我试图从mysql数据库中获取数据,但我一直遇到这个错误。我似乎不知道出了什么问题。这是我的代码和日志 日志: PHP代码:Java 分析mysql数据库中的数据时出错,java,php,android,mysql,json,Java,Php,Android,Mysql,Json,我正在开发一个android应用程序。我试图从mysql数据库中获取数据,但我一直遇到这个错误。我似乎不知道出了什么问题。这是我的代码和日志 日志: PHP代码: <?php $response = array(); $response["success"] = 0; $response["message"] = "No routes found"; // include db connect class include('db.php'); // connecting to db $
<?php
$response = array();
$response["success"] = 0;
$response["message"] = "No routes found";
// include db connect class
include('db.php');
// connecting to db
$db = new DB_CONNECT();
// get all routes from route table
$result = mysql_query("SELECT * FROM route");
// check for empty result
if ($result && mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["routes"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$route = array();
$route["busRouteId"] = $row["busRouteId"];
$route["routeName"] = $row["routeName"];
// push single product into final response array
array_push($response["routes"], $route);
}
// success
$response["success"] = 1;
}
// Echo JSON anyway!
echo json_encode($response);
die();
?>
主要活动:
public class MainActivity extends Activity {
DBHelper db;
int routeloadsuccess;
JSONArray jArray = null
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinroute= (Spinner)findViewById(R.id.spinnerroute);
pb = (ProgressBar) findViewById(R.id.progressBar);
pb.setVisibility(View.INVISIBLE);
db = new DBHelper(this);
}
public void shout(String voice)
{Toast.makeText(this, voice,Toast.LENGTH_LONG).show();}
public void insertNewRoutes(int id, String routename){
//check if route exists
boolean exists = db.checkRouteforId(id), result = false;
if(exists)
{
result = db.updateRoute(id, routename);
}
else{ result = db.insertRoute(id, routename);}
if(result){routeloadsuccess = 1;}
}
private class Load_Routes extends AsyncTask<String, String, JSONObject>{
InputStream is;
String url;
private static final String TAG_ROUTE = "bus_route";
public Load_Routes()
{
is = null;
url = "http://busscheduledata.com/RouteMobile.php";
}
@Override
protected JSONObject doInBackground(String... arg0) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
shout("Loading Routes...");
pb.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(JSONObject json) {
// TODO Auto-generated method stub
pb.setVisibility(View.INVISIBLE);
try {
// Getting JSON Array
int success = json.getInt("success");
if (success == 1) {
JSONArray JAStuff = json.getJSONArray("response");
/** CHECK THE NUMBER OF RECORDS **/
int intStuff = JAStuff.length();
if (intStuff != 0) {
for (int i = 0; i < JAStuff.length(); i++) {
JSONObject JOStuff = JAStuff.getJSONObject(i);
Log.e("ALL THE STUFF", JOStuff.toString());
// Storing JSON item in a Variable
int routeid = JOStuff.getInt("busRouteId");
String routename = JOStuff.getString("routeName");
//Set JSON Data in spinner
insertNewRoutes(routeid,routename );
}
}
}
} catch (JSONException e) {
e.printStackTrace();
shout("Loading failed");
}
}
}//end of loading route thingy
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
populateMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
applyMenuChoice(item);
return(applyMenuChoice(item) ||super.onOptionsItemSelected(item));
}
private void populateMenu(Menu menu) {
menu.add(0, SIXTEEN_ID, 0, "Get Routes");
}
private boolean applyMenuChoice(MenuItem item) {
Intent intent;
switch (item.getItemId())
{
case SIXTEEN_ID:
db.deleteAllRoutes();
Load_Routes lr = new Load_Routes();
lr.execute();
if(routeloadsuccess == 1){
shout("Loading Complete");
intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);}
return(true);
//break;
}
return true ;
}
}
公共类MainActivity扩展活动{
dbdb;
int routeloadsuccess;
JSONArray jArray=null
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinroute=(Spinner)findViewById(R.id.spinnerroute);
pb=(ProgressBar)findViewById(R.id.ProgressBar);
pb.setVisibility(视图不可见);
db=新的DBHelper(此);
}
公共广播(弦乐)
{Toast.makeText(this,voice,Toast.LENGTH_LONG).show();}
public void insertNewRoutes(int-id,String-routename){
//检查路线是否存在
boolean exists=db.checkRouteforId(id),result=false;
如果(存在)
{
结果=db.updateRoute(id,routename);
}
else{result=db.insertRoute(id,routename);}
如果(结果){routeloadsuccess=1;}
}
私有类加载\路由扩展异步任务{
输入流为;
字符串url;
专用静态最终字符串TAG_ROUTE=“bus_ROUTE”;
公共载货路线()
{
is=null;
url=”http://busscheduledata.com/RouteMobile.php";
}
@凌驾
受保护的JSONObject doInBackground(字符串…arg0){
JSONParser jParser=新的JSONParser();
JSONObject json=jParser.getJSONFromUrl(url);
返回json;
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
喊叫(“装货路线…”);
pb.setVisibility(View.VISIBLE);
}
@凌驾
受保护的void onPostExecute(JSONObject json){
//TODO自动生成的方法存根
pb.setVisibility(视图不可见);
试一试{
//获取JSON数组
int success=json.getInt(“success”);
如果(成功==1){
JSONArray JAStuff=json.getJSONArray(“响应”);
/**检查记录的数量**/
int intStuff=JAStuff.length();
if(intStuff!=0){
对于(int i=0;i
此行正在打印:E/响应字符串:n
您的响应是“n”,这是字符串。在下一行中,您将把它转换为一个JsonObject,它肯定会抛出异常
jObj = new JSONObject(json);
将服务器响应从字符串更改为Json
此行正在打印:E/响应字符串:n
您的响应是“n”,这是字符串。在下一行中,您将把它转换为一个JsonObject,它肯定会抛出异常
jObj = new JSONObject(json);
将服务器的响应从String更改为Json那么,您是否记录了正在获取的Json?这是要做的第一件事——找出问题是在PHP还是Java中。这样你就可以忽略正在工作的那一半。也许可以在异常发生的地方发布类?”在com.example.busscheduledatarecorder.MainActivity$Load_Routes.onPostExecute(MainActivity.java:407)”中,JSONParser成员不应该是静态的,我强烈建议不要自己编写JSON解析器。大多数人使用jackson或gson.Post MainActivity.java的代码错误在MainActivity中,这里是获取NullPointerException的地方。如果您想要答案,请将MainActivity.java发布到此处。您是否记录了获取的JSON?这是要做的第一件事——找出问题是在PHP还是Java中。这样你就可以忽略正在工作的那一半。也许可以在异常发生的地方发布类?”在com.example.busscheduledatarecorder.MainActivity$Load_Routes.onPostExecute(MainActivity.java:407)”中,JSONParser成员不应该是静态的,我强烈建议不要自己编写JSON解析器。大多数人使用jackson或gson.Post MainActivity.java的代码错误在MainActivity中,这里是获取NullPointerException的地方。如果需要答案,请在此处发布MainActivity.java
Log.e("response string",json);
jObj = new JSONObject(json);