2011-01-11 36 views
0

因此我使用了遍佈網絡的代碼示例,並讓我的應用程序準確地調用服務器上的.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將不得不返回一個數組類型或什麼?

任何幫助表示讚賞,謝謝。

回答

0

首先,你不應該在UI線程上進行服務器調用。使用AsyncTaskService在單獨的線程上進行遠程調用。

接下來,我不知道返回的Json的確切結構,但它聽起來像要將其轉換爲更乾淨的對象模型。只需創建一個對象,該對象對於Json數組中的每個條目都有一個字段,並用賦值語句替換您的日誌語句。 I.e:

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; 
} 
+0

我照你所描述的做,並返回結果,這是我的類位置的數組。快速的問題,我怎樣才能訪問數據,一旦返回像result.get(位置);? – bMon 2011-01-11 17:37:10