如何通过远程数据库检索数据后操作数据

时间:2011-01-11 00:46:50

标签: java android types maps

所以我使用了来自网络的代码示例,让我的应用程序准确地调用服务器上的.php文件,检索JSON数据,然后解析数据并打印出来。

问题是它只是为了我正在关注的教程打印到屏幕上,但现在我需要在其他地方使用这些数据,并需要帮助找出这个过程。

最终目标是使用地图坐标返回我的数据库查询,然后在谷歌地图上绘制它们。我有另一个应用程序,我在地图上手动绘制点,所以我将集成这个应用程序,一旦我可以了解如何正确操作返回的数据。

        public class Remote extends Activity {
    /** Called when the activity is first created. */

       TextView txt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Create a crude view - this should really be set via the layout resources
        // but since its an example saves declaring them in the XML.
        LinearLayout rootLayout = new LinearLayout(getApplicationContext());
        txt = new TextView(getApplicationContext());
        rootLayout.addView(txt);
        setContentView(rootLayout);

        // Set the text and call the connect function.
        txt.setText("Connecting...");
        //call the method to run the data retreival
        txt.setText(getServerData(KEY_121));


    }
    public static final String KEY_121 = "http://example.com/mydbcall.php";

    private String getServerData(String returnString) {

       InputStream is = null;

       String result = "";
        //the year data to send
        //ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //nameValuePairs.add(new BasicNameValuePair("year","1970"));

        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(KEY_121);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response to string
        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();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        //parse json data

        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","longitude: "+json_data.getDouble("longitude")+
                                ", latitude: "+json_data.getDouble("latitude")
                        );
                        //Get an output to the screen
                        returnString += "\n\t" + jArray.getJSONObject(i);
                }
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        return returnString;
    }   

    }

所以代码:

returnString += "\n\t" + jArray.getJSONObject(i);

是当前正在打印到屏幕上的内容。

我必须弄清楚如何将数据转化为我可以在程序中的其他位置引用的内容,并访问各个元素 即:

double longitude = jArray.getJSONObject(3).longitude; 

或者那种效果......

我认为类getServerData必须返回一个Array类型?

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您不应该在UI线程上进行服务器调用。使用AsyncTaskService在单独的线程上进行远程调用。

接下来,我不知道您返回的Json的确切结构,但听起来您想将其转换为更干净的对象模型。只需创建一个对象,该对象具有Json数组中每个条目的字段,并使用赋值语句替换日志语句。即:

class Location {
  private double latitude;
  private double longitude;
  public Location(double latitude, double longitude) { ....}
}

private List<Location> getServerData(String returnString) {
 List<Location> result = new ArrayList<Location>();
 JSONArray jArray = new JSONArray(result);
 for(int i=0;i<jArray.length();i++){
    Location loc = new Location(json_data.getDouble("latitude"), json_data.getDouble("longitude"));
    result.add(loc);
 }
 return result;
}