2011-06-10 93 views
1

在下面的list.java中,我製作了一個抓取當前位置並將其添加到URL的底部附近的URL。我有一個PHP腳本從URL中獲取位置,然後執行它的操作並返回一個基於JSON的數組。我需要獲取要在我的代碼的oncreate區域中的HttpPost訪問的URL字符串。我怎麼能通過這個信息?

public class List extends ListActivity { 

    int ct_id; 
    String[] ct_number = null; 
    String[] ct_address = null; 
    String[] ct_phone = null; 
    String[] ct_fax = null; 
    String[] ct_email = null; 
    String[] ct_city = null; 
    String[] ct_province = null; 
    String[] ct_country = null; 
    String[] ct_pcode = null; 
    String[] ct_lat = null; 
    String[] ct_long = null; 
    String[] ct_distance = null; 
    String[] ct_show = null; 
    String[] ct_listinfo = null; 

    MyLocation myLocation = new MyLocation(); 

    public void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 
     setContentView(R.layout.timslist); 
     findCurrentLocation(); 

     String result = null; 
     InputStream is = null; 
     StringBuilder sb = null; 
     //http post 
     try{ 

      HttpClient httpclient = new DefaultHttpClient(); 

        //This is where i need the myurl string to be accessed 
      HttpPost httppost = new HttpPost(Need_the_myurl_String_here); 

      //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      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); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 
      String line="0"; 
      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()); 
     } 
     //paring data 
     JSONArray jArray; 
     try{ 
      jArray = new JSONArray(result); 
      JSONObject json_data=null; 
      ct_number=new String[jArray.length()]; 
      ct_address=new String[jArray.length()]; 
      ct_phone=new String[jArray.length()]; 
      ct_fax=new String[jArray.length()]; 
      ct_email=new String[jArray.length()]; 
      ct_city=new String[jArray.length()]; 
      ct_province=new String[jArray.length()]; 
      ct_country=new String[jArray.length()]; 
      ct_pcode=new String[jArray.length()]; 
      ct_lat=new String[jArray.length()]; 
      ct_long=new String[jArray.length()]; 
      ct_distance=new String[jArray.length()]; 
      ct_listinfo=new String[jArray.length()]; 
      for(int i=0;i<jArray.length();i++){ 
       json_data = jArray.getJSONObject(i); 
       ct_id=json_data.getInt("location_id"); 
       ct_number[i]=json_data.getString("store_number"); 
       ct_address[i]=json_data.getString("store_address"); 
       ct_phone[i]=json_data.getString("store_phone"); 
       ct_fax[i]=json_data.getString("store_fax"); 
       ct_email[i]=json_data.getString("store_email"); 
       ct_city[i]=json_data.getString("store_city"); 
       ct_province[i]=json_data.getString("store_province"); 
       ct_country[i]=json_data.getString("store_country"); 
       ct_pcode[i]=json_data.getString("store_pcode"); 
       ct_lat[i]=json_data.getString("store_lat"); 
       ct_long[i]=json_data.getString("store_long"); 
       ct_distance[i]=json_data.getString("store_distance"); 
       ct_listinfo[i] = new String (ct_address[i] + "\n" + ct_city[i] + ", " + ct_province[i] + " - " + ct_distance[i] + " Km Away"); 
      } 
     } 
     catch(JSONException e1){ 
      Toast.makeText(getBaseContext(), "No Locations Found" ,Toast.LENGTH_LONG).show(); 
     } catch (ParseException e1) { 
      e1.printStackTrace(); 
     } 

     setListAdapter(new ArrayAdapter<String>(this,R.layout.listview,ct_listinfo)); 
     ListView lv; 
     lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setCacheColorHint(Color.TRANSPARENT); 
     lv.setBackgroundColor(Color.TRANSPARENT); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> timslist, View view, 
        int position, long id) { 

       Intent i = new Intent(getApplicationContext(), ListMore.class); 

       i.putExtra("ct_number_pass", ct_number[position]); 
       i.putExtra("ct_address_pass", ct_address[position]); 
       i.putExtra("ct_phone_pass", ct_phone[position]); 
       i.putExtra("ct_city_pass", ct_city[position]); 
       i.putExtra("ct_province_pass", ct_province[position]); 
       i.putExtra("ct_country_pass", ct_country[position]); 
       i.putExtra("ct_pcode_pass", ct_pcode[position]); 
       i.putExtra("ct_distance_pass", ct_distance[position]); 

       startActivity(i); 

      } 
     }); 
    } 

    private void findCurrentLocation() { 
     myLocation.getLocation(this, locationResult); 
    } 

    public LocationResult locationResult = new LocationResult() { 

     @Override 
     public void gotLocation(Location location) { 
      // TODO Auto-generated method stub 
      if (location != null) { 

        //Here is the String i am trying to access 
       String myurl = "http://www.doamin.com/list.php?lat=" + location.getLatitude() + "&long=" + location.getLongitude(); 

      } 
     } 
    }; 
} 

我曾嘗試與他人的幫助,但不能似乎得到這個工作的權利,所以我想看看是否有一個簡單而可行的解決方案在那裏。

+0

不知道我是否理解,但您想在GPS位置(當調用gotLocation()時)執行POST請求? – 2011-06-10 20:05:32

+0

是的'gotLocation()'的URL需要能夠使用'HttpPost httppost = new HttpPost(Need_the_myurl_String_here);' – PsychoDogg 2011-06-10 20:11:00

+1

爲什麼你不把你的代碼放在你需要在一個單獨的方法上調用,並調用方法String myurl =「http://www.doamin.com/list.php?lat=」+ location.getLatitude()+「&long =」+ location.getLongitude(); ? – 2011-06-10 20:16:46

回答

0

您不能這樣做:在返回給您之前,您正試圖訪問位置值。您應該:

  1. 將Handler實例添加到您的類中。
  2. 將HttpClient/HttpPost調用移入單獨的方法。
  3. 使用位置數據從gotLocation方法發送消息給處理程序。
  4. 從處理程序調用上述步驟3中的位置數據。

編輯:嘗試創建這樣的方法:

void updateData(String url) 
{ 
     String result = null; 
     InputStream is = null; 
     StringBuilder sb = null; 
     //http post 
     try{ 

      HttpClient httpclient = new DefaultHttpClient(); 

        //This is where i need the myurl string to be accessed 
      HttpPost httppost = new HttpPost(url); 

      //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      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); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 
      String line="0"; 
      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()); 
     } 
     //paring data 
     JSONArray jArray; 
     try{ 
      jArray = new JSONArray(result); 
      JSONObject json_data=null; 
      ct_number=new String[jArray.length()]; 
      ct_address=new String[jArray.length()]; 
      ct_phone=new String[jArray.length()]; 
      ct_fax=new String[jArray.length()]; 
      ct_email=new String[jArray.length()]; 
      ct_city=new String[jArray.length()]; 
      ct_province=new String[jArray.length()]; 
      ct_country=new String[jArray.length()]; 
      ct_pcode=new String[jArray.length()]; 
      ct_lat=new String[jArray.length()]; 
      ct_long=new String[jArray.length()]; 
      ct_distance=new String[jArray.length()]; 
      ct_listinfo=new String[jArray.length()]; 
      for(int i=0;i<jArray.length();i++){ 
       json_data = jArray.getJSONObject(i); 
       ct_id=json_data.getInt("location_id"); 
       ct_number[i]=json_data.getString("store_number"); 
       ct_address[i]=json_data.getString("store_address"); 
       ct_phone[i]=json_data.getString("store_phone"); 
       ct_fax[i]=json_data.getString("store_fax"); 
       ct_email[i]=json_data.getString("store_email"); 
       ct_city[i]=json_data.getString("store_city"); 
       ct_province[i]=json_data.getString("store_province"); 
       ct_country[i]=json_data.getString("store_country"); 
       ct_pcode[i]=json_data.getString("store_pcode"); 
       ct_lat[i]=json_data.getString("store_lat"); 
       ct_long[i]=json_data.getString("store_long"); 
       ct_distance[i]=json_data.getString("store_distance"); 
       ct_listinfo[i] = new String (ct_address[i] + "\n" + ct_city[i] + ", " + ct_province[i] + " - " + ct_distance[i] + " Km Away"); 
      } 
     } 
     catch(JSONException e1){ 
      Toast.makeText(getBaseContext(), "No Locations Found" ,Toast.LENGTH_LONG).show(); 
     } catch (ParseException e1) { 
      e1.printStackTrace(); 
     } 

     setListAdapter(new ArrayAdapter<String>(this,R.layout.listview,ct_listinfo)); 
     ListView lv; 
     lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setCacheColorHint(Color.TRANSPARENT); 
     lv.setBackgroundColor(Color.TRANSPARENT); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> timslist, View view, 
        int position, long id) { 

       Intent i = new Intent(getApplicationContext(), ListMore.class); 

       i.putExtra("ct_number_pass", ct_number[position]); 
       i.putExtra("ct_address_pass", ct_address[position]); 
       i.putExtra("ct_phone_pass", ct_phone[position]); 
       i.putExtra("ct_city_pass", ct_city[position]); 
       i.putExtra("ct_province_pass", ct_province[position]); 
       i.putExtra("ct_country_pass", ct_country[position]); 
       i.putExtra("ct_pcode_pass", ct_pcode[position]); 
       i.putExtra("ct_distance_pass", ct_distance[position]); 

       startActivity(i); 

      } 
     }); 

} 

void updateList(final String url) 
{ 
    getListView().post(new Runnable(){ public void run(){ updateData(url); }}); 
} 

,然後爲此在gotLocation

@Override 
     public void gotLocation(Location location) { 
      // TODO Auto-generated method stub 
      if (location != null) { 

        //Here is the String i am trying to access 
        updateList("http://www.doamin.com/list.php?lat=" + location.getLatitude() + "&long=" + location.getLongitude()); 
      } 
     } 

你需要修復的任何編譯錯誤,但類似這應該適合你。

+0

每次我移動HTTPPost,這都會破壞我的代碼,並且這個列表不起作用。我不明白我會如何去做這件事。 – PsychoDogg 2011-06-10 20:58:08

+0

'該方法的返回類型上缺少令牌「URL」 語法錯誤,VariableDeclaratorId預計在此之後令牌 類型新的Runnable(){}必須實現繼承的抽象方法Runnable.run() 網址不能解決了一個類型「 任何想法? – PsychoDogg 2011-06-10 21:42:06

+0

我一直在玩你的例子,但似乎無法得到它的工作,我已經回到我的發佈的代碼,希望找到一種方法來使這項工作。我不明白的是爲什麼我得到上述錯誤或如何解決它們。我想我想要的是一件簡單的事情。也許我應該重新看看讓我的腳本獲得當前位置並繼續使用HttpPost的選項? – PsychoDogg 2011-06-11 00:23:01