2013-05-01 54 views
0

嗨我建設拉動使用GPS我的地址的應用程序的.txt文件,然後把它比作一個地址我在一個網站保存爲.txt文件。它很好地拉動.txt文件。然後GPS獲取經緯度很好,並反向地理編碼罰款。我輸入了完美的txt,然後使用相對佈局將兩個文本視圖放在彼此的頂部,以查看是否可以找到差異。我也想過,也許,網絡文件在這一點上不是文字,但可能是某種xml,所以我將它轉換爲字符串,將它們放在另一個textview中,然後將這兩個字符串作爲字符串進行比較,但仍不會將它們顯示爲比賽。比較字符串,從網站的android

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文件是我的地址,我不想在互聯網上,所以我已經刪除了我使用的網址。

+0

我覺得\ n個字符可能會導致您的問題刪除它們,嘗試從兩個服務器刪除響應和GPS數據 – Rotem 2013-05-01 09:49:53

+0

工作,非常感謝你,我一直在搞這個3天,如果你張貼它作爲答案我可以接受它。 – 2013-05-01 10:09:24

回答

0

我覺得\ n個字符可能會導致您的問題,嘗試從兩個服務器的響應和GPS數據