将字符串与android网站的.txt文件进行比较
嗨,我正在构建一个应用程序,它使用gps提取我的地址,然后将其与我保存在网站中的.txt文件中的地址进行比较。它可以很好地提取.txt文件。然后gps会得到lat和long fine以及反向地理编码fine。我输入了txt perfect,然后使用相对布局将两个文本视图放在彼此的顶部,看看是否可以找到差异。我还认为,可能web文件在这一点上不是文本,但可能是某种xml,所以我将其转换为字符串,将它们放在另一个textview中,然后将这两个作为字符串进行比较,但仍然不会将它们显示为匹配项将字符串与android网站的.txt文件进行比较,android,gps,location,Android,Gps,Location,嗨,我正在构建一个应用程序,它使用gps提取我的地址,然后将其与我保存在网站中的.txt文件中的地址进行比较。它可以很好地提取.txt文件。然后gps会得到lat和long fine以及反向地理编码fine。我输入了txt perfect,然后使用相对布局将两个文本视图放在彼此的顶部,看看是否可以找到差异。我还认为,可能web文件在这一点上不是文本,但可能是某种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);
}
}
公共类MainActivity扩展活动{
按钮比较器按钮;
按钮地址按钮;
文本视图位置文本;
文本查看地址文本;
TextView internetText;
TextView地址text2;
TextView InternetExt2;
位置当前位置;
双流纬度;
双经度;
国际网络计数器;
@凌驾
创建时的公共void(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=(按钮)findViewById(R.id.addressButton);
compareButton=(按钮)findViewById(R.id.compareButton);
addressText2=(TextView)findViewById(R.id.addressText2);
internetText=(TextView)findViewById(R.id.internetText);
internetText2=(TextView)findViewById(R.id.internetText2);
网络计数器=0;
changewebaddress();
getWebAddress();
getLatLong();
this.addressButton.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
getAddress();
getWebAddress();
转换地址();
}
});
this.compareButton.setOnClickListener(新的OnClickListener()文件){
公共void onClick(视图v){
compareAddress();
getWebAddress();
}
});
}
void getAddress(){
试一试{
Geocoder gcd=新的Geocoder(这个,Locale.getDefault());
列表地址=
gcd.getFromLocation(当前纬度,当前经度,100);
如果(地址.size()>0){
StringBuilder结果=新建StringBuilder();
对于(int i=0;i 对于(int x=0;x我认为\n字符可能会导致您的问题,请尝试从服务器响应和gps数据中删除它们我认为\n字符可能会导致您的问题,请尝试从服务器响应和gps数据中删除它们。非常感谢,如果您将其作为a发布,我已经处理了3天了我可以接受。