将字符串与网站android中的.txt文件进行比较

时间:2013-05-01 09:42:02

标签: android gps location

您好我正在构建一个应用程序,它使用gps拉取我的地址,然后将其与我保存到网站中的.txt文件的地址进行比较。它拉.txt文件很好。然后gps得到拉特和长细,并且反转地理编码也很好。我输入了完美的txt,我使用相对布局将两个文本视图放在彼此的顶部,看看我是否能找到差异。我也想过,也许这个网页文件不是文本,但也许是某种xml,所以我将它转换为字符串,将它们放在另一个textview中,然后将这两个文件作为字符串进行比较,但仍然不会将它们显示为比赛。

    public class MainActivity extends Activity {

Button compareButton;
Button addressButton;
TextView locationText;
TextView addressText;
TextView internetText;
TextView addressText2;
TextView internetText2;
Location currentLocation;
double currentLatitude;
double currentLongitude;
int webCounter;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    StrictMode.enableDefaults();

    addressText = (TextView)findViewById(R.id.addressText);
    locationText = (TextView)findViewById(R.id.locationText);
    addressButton = (Button)findViewById(R.id.addressButton);
    compareButton = (Button)findViewById(R.id.compareButton);
    addressText2 = (TextView)findViewById(R.id.addressText2);
    internetText = (TextView) findViewById(R.id.internetText);
    internetText2 = (TextView) findViewById(R.id.internetText2);
    webCounter = 0;

    changeWebAdress();
    getWebAdress();
    getLatLong();

        this.addressButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                getAddress();
                getWebAdress();
                convertAdress();

            }
        });
        this.compareButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v){
                compareAddress();
                getWebAdress();
            }
        }); 
}

void getAddress(){
    try{
        Geocoder gcd = new Geocoder(this, Locale.getDefault());
        List<Address> addresses = 
            gcd.getFromLocation(currentLatitude, currentLongitude,100);
        if (addresses.size() > 0) {
            StringBuilder result = new StringBuilder();
            for(int i = 0; i < addresses.size(); i++){
                Address address =  addresses.get(i);
                int maxIndex = address.getMaxAddressLineIndex();
                for (int x = 0; x <= maxIndex; x++ ){
                    result.append(address.getAddressLine(x));
                    result.append(",");
                }               
                result.append(address.getLocality());
                result.append(",");
                result.append(address.getPostalCode());
                result.append("\n\n");
            }
            addressText.setText(result.toString());

        }
      }
    catch(IOException ex){
        addressText.setText(ex.getMessage().toString());
    }
}

void updateLocation(Location location){
    currentLocation = location;
    currentLatitude = currentLocation.getLatitude();
    currentLongitude = currentLocation.getLongitude();
    locationText.setText(""+currentLatitude + ", " + currentLongitude);
}

    private void compareAddress(){

    String adress2;
    String internet2;

    adress2 = addressText2.getText().toString();
    internet2 =internetText2.getText().toString();

    if (adress2.equalsIgnoreCase(internet2)){
        compareButton.setText("like it on FaceBook");
    }

    //else{
        //changeWebAdress();
    //}
}

private void convertAdress(){

    internetText2.setVisibility(View.GONE);
    addressText2.setVisibility(View.GONE);

    internetText2.setText(internetText.getText().toString());
    addressText2.setText(addressText.getText().toString());

}

private void changeWebAdress(){

    webCounter = webCounter+1;

    if (webCounter == 100){

        webCounter = 0;

    }


}

private void getWebAdress(){


    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httppost = new HttpGet("https://webAdress"+webCounter+".txt");
    HttpResponse response = null;
    try {
        response = httpclient.execute(httppost);
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
            HttpEntity ht = response.getEntity();

            BufferedHttpEntity buf = null;
            try {
                buf = new BufferedHttpEntity(ht);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            InputStream is = null;
            try {
                is = buf.getContent();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            BufferedReader r = new BufferedReader(new InputStreamReader(is));

            StringBuilder total = new StringBuilder();
            String line;
            try {
                while ((line = r.readLine()) != null) {
                    total.append(line + "\n");
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            internetText.setText(total.toString()); 
}

    private void getLatLong(){

        LocationManager locationManager = 
            (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                updateLocation(location); 

            }
            public void onStatusChanged(
                    String provider, int status, Bundle extras) {}
            public void onProviderEnabled(String provider) {}
            public void onProviderDisabled(String provider) {}
        };

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    }
}

我知道我需要让互联网收集asyncTask。我打算这样做,如果我可以得到这个功能。现在我正在以严格的模式运行它。 txt文件是我的地址,我不想在互联网上,所以我删除了我正在使用的网址。

1 个答案:

答案 0 :(得分:0)

我认为\ n字符可能会导致您的问题,尝试从服务器响应和gps数据中删除它们