首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在公共LocationResult之外访问字符串信息

在公共LocationResult之外访问字符串信息
EN

Stack Overflow用户
提问于 2011-06-10 08:02:49
回答 2查看 734关注 0票数 0

我有一个脚本,如下所示,从LocationResult获取url以在httppost中使用

代码语言:javascript
复制
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();

        //check for key to remove ads
        if(isKeyInstalled(this) == false){

            //AdMob Banner
            LinearLayout parent = (LinearLayout) findViewById(R.id.adveiw);
            AdView ad = new AdView(this, AdSize.BANNER, "XXXXXXXXXXXXXX");
            parent.addView(ad);
            AdRequest r = new AdRequest();
            //r.setTesting(true);
            r.setTesting(false);
            ad.loadAd(r);
        }

        if(isKeyInstalled(this) == true){

        }

        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("" + infourl + "");
            //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 Addresses 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(), TimsListMore.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);

            }
        });
    }

    protected boolean isKeyInstalled(Context context) {
          // the packagename of the 'key' app
          String proPackage = "XXXXXXXXXXXXXXXXXXXX";

          // get the package manager
          final PackageManager pm = context.getPackageManager();

          // get a list of installed packages
          List<PackageInfo> list = pm.getInstalledPackages(PackageManager.GET_DISABLED_COMPONENTS);

          // let's iterate through the list
          Iterator<PackageInfo> i = list.iterator();
          while(i.hasNext()) {
            PackageInfo p = i.next();
            // check if proPackage is in the list AND whether that package is signed
            //  with the same signature as THIS package
            if((p.packageName.equals(proPackage)) &&
               (pm.checkSignatures(context.getPackageName(), p.packageName) == PackageManager.SIGNATURE_MATCH))
              return true;
          }
          return false;
        }

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

    public LocationResult locationResult = new LocationResult() {


        public void gotLocation(Location location) {
            // TODO Auto-generated method stub
            if (location != null) {
                String infourl  = "http://www.somedomain.com/list.php?lat=" + location.getLatitude() + "&long=" + location.getLongitude();

            }
        }
    };
}

所以这就是

代码语言:javascript
复制
String infourl  = "http://www.somedomain.com/list.php?lat=" + location.getLatitude() + "&long=" + location.getLongitude();

需要深入研究

代码语言:javascript
复制
String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("" + infourl + "");
            //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());
        }

我认为这不是一件很难做的事情。

EN

回答 2

Stack Overflow用户

发布于 2011-06-10 08:20:34

我的建议是尝试使用公共字符串...希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 2011-06-10 08:23:31

以下是上一个问题:

代码语言:javascript
复制
public LocationResult locationResult = new LocationResult() {
  String latitude = null;  
  String longitude = null;  

  public void gotLocation(Location location) {
    if (location != null) {
      latitude  = location.getLatitude() + "";
      longitude  = location.getLongitude() + "";
    }
  }

  public String getLatitude() {
    return latitude;
  }
  public String getLongitude() {
    return longitude;
  }
}

您将使用它作为

代码语言:javascript
复制
String location = new String (locationResult.getLatitude() + " " + locationResult.getLongitude());

在你活动中的任何地方。当然,这非常粗糙,甚至根本不检查是否存在有效的位置,但它给了您解决它的想法。

再想一想,我相信您真正想要的与您所要求的恰恰相反:如果您需要使用LAT值和long值来发送http post,那么我将在gotLocation函数中执行post。这样,您就可以确定不会向post发送空值。创建一个方法

代码语言:javascript
复制
public void postURL (String urlString) {
  if (urlString == null) return;
  HttpPost httppost = new HttpPost(urlString);
  // whatever else code you have to handle responses, etc.
}

并从gotLocation()中调用它为

代码语言:javascript
复制
TimsList.this.postURL ("http://mytimmies.myfunhouse.net/app_main_list.php?lat=" + 
  location.getLatitude() + "&long=" + location.getLongitude());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6300624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档