Android使用JSON检索SQL查询时遇到困难

时间:2014-02-23 00:46:32

标签: java android sql json

我在从xampp sql server检索坐标时遇到了一些麻烦。当我在手机上输入网址(http://192.168.1.100/cwdb/get_all_coordinates.php)时,我会得到这个JSON坐标列表:

{"coordinates":[{"cid":"1","latitude":"36.544670","longitude":"36.544651"},{"cid":"2","latitude":"36.544270","longitude":"15.524650"},{"cid":"3","latitude":"37.544270","longitude":"16.524651"},{"cid":"4","latitude":"27.544270","longitude":"76.524651"},{"cid":"5","latitude":"27.523569","longitude":"31.146650"}],"success":1}

然而,当我尝试通过我的Android应用程序检索这个时,我遇到了下载JSON字符串的问题。这是发生问题的代码。 System.out.println("LINE:"+line);正在打印出“LINE:null”

我已在我的清单文件中包含了互联网权限。这是我的代码

private class JSONCoordinates extends AsyncTask<String, Void, ArrayList<Coordinate>> {

    @Override
    protected ArrayList<Coordinate> doInBackground(String... params) {
        String line = null;
        ArrayList<Coordinate> cList = new ArrayList<Coordinate>();
        //String data = ( (new WeatherHttpClient()).getWeatherData());
        try {           
         HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet(BASE_URL);
            HttpResponse response = httpClient.execute(httpGet, localContext);
            String result = "";

            BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                  response.getEntity().getContent()
                )
              );

            //String line = null;
            while ((line = reader.readLine()) != null){
              result += line + "\n";
            }
            System.out.println("LINE:"+line);
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
        try {
            cList = JSONParser.getCoordinate(line);
        } catch (JSONException e) {             
            e.printStackTrace();
        }
        return cList;
}




    FATAL EXCEPTION: AsyncTask #1
     java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
        at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
        at org.json.JSONTokener.nextValue(JSONTokener.java:94)
        at org.json.JSONObject.<init>(JSONObject.java:154)
        at org.json.JSONObject.<init>(JSONObject.java:171)
        at com.example.citywalks.JSONParser.getCoordinate(JSONParser.java:17)
        at com.example.citywalks.MainActivity$JSONCoordinates.doInBackground(MainActivity.java:78)
        at com.example.citywalks.MainActivity$JSONCoordinates.doInBackground(MainActivity.java:1)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
     E/AndroidRuntime(29017):   ... 5 more

1 个答案:

答案 0 :(得分:0)

line为null ...因为只有当它变为null时,while循环才会终止。你应该打印结果变量。