无法使用和从MySQL数据库中检索数据

时间:2014-02-02 04:35:47

标签: php android mysql database json

我正在尝试根据用户提供的一些特定参数从我的数据库中检索学校列表。我有很多代码,但我无法找到我的错误并需要一些帮助。我花了6个多小时研究并尝试解决这个问题。我似乎无法修复它。我将不胜感激任何帮助。

这是一个连接到MySQL数据库btw的Android应用程序。

以下是我点击按钮时启动搜索的代码:

btnGetSchool.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String city = inputCity.getText().toString();
                String school = inputSchoolName.getText().toString();
                String state = chooseState.getSelectedItem().toString();
                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.getSchool(school, state, city);



                try {
                    schoolText.setText(json.getString(""));

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }


        });

这是我的UserFunctions.java代码:

public JSONObject getSchool(String school, String state, String city) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("school", school));
        params.add(new BasicNameValuePair("state", state));
        params.add(new BasicNameValuePair("city", city));


        JSONObject json = jsonParser.getJSONFromUrl(getSchoolURL, params);
        return json;
    }

我的JSON解析器:

package com.fbla.sap;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", 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
        return jObj;

    }
}

最后,我的PHP代码实际搜索学校。我觉得这个错误可能源于此,因为我对PHP很新,并且不完全理解它:

public function schoolSearch($school, $state, $city){

        $schsearch=mysql_query("
                SELECT `sch_name`, `sch_id`,sch_state`,`sch_city`
                FROM `schools`
                WHERE (`sch_name`=$school) AND (`sch_state`=$state) AND (`sch_city`=$city)
            ");
            /**$searchresult=array();-
            while($schresult=mysql_fetch_array($schsearch)) {

                array_push($searchresult, $schresult['sch_id']."=>".$schresult['sch_name']);


            }**/

            return mysql_fetch_array($schresult);

            /**if (count($searchresult) > 0) {
                return $searchresult;
            } else {
                return false;
            }**/


    }

总结一下,我将添加错误:

02-01 23:32:03.260: E/AndroidRuntime(12803): FATAL EXCEPTION: main
02-01 23:32:03.260: E/AndroidRuntime(12803): java.lang.NullPointerException
02-01 23:32:03.260: E/AndroidRuntime(12803):    at com.fbla.sap.CreateAccFragment$2.onClick(CreateAccFragment.java:148)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at android.view.View.performClick(View.java:4475)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at android.view.View$PerformClick.run(View.java:18784)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at android.os.Handler.handleCallback(Handler.java:730)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at android.os.Looper.loop(Looper.java:137)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at android.app.ActivityThread.main(ActivityThread.java:5450)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at java.lang.reflect.Method.invokeNative(Native Method)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at java.lang.reflect.Method.invoke(Method.java:525)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-01 23:32:03.260: E/AndroidRuntime(12803):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

更改

return mysql_fetch_array($schresult);

return mysql_fetch_array($schsearch);