2012-10-14 201 views
0

你好我在文本文件存儲經度和緯度值,並將其保存在我的sdcard.now我想這個數據保存到網絡服務器(JAVA)textfile.please告訴我如何創建服務器中的textfile以及如何將數據發佈到該文件。這裏是我的代碼。如何從文本文件上傳數據到服務器的文本文件

public class MainActivity extends Activity implements LocationListener{ 

private final static String STORETEXT="storetext.txt"; 


LocationManager locationManager ; 
String provider; 
String value1; 
String value2; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Getting LocationManager object 
    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);   

    // Creating an empty criteria object 
    Criteria criteria = new Criteria(); 

    // Getting the name of the provider that meets the criteria 
    provider = locationManager.getBestProvider(criteria, false); 


    if(provider!=null && !provider.equals("")){ 

     // Get the location from the given provider 
     Location location = locationManager.getLastKnownLocation(provider); 


     locationManager.requestLocationUpdates(provider, 20000, 1, this); 


     if(location!=null) 
      onLocationChanged(location); 
     else 
      Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show(); 

    }else{ 
     Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

@Override 
public void onLocationChanged(Location location) { 
    // Getting reference to TextView tv_longitude 
    TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude); 

    // Getting reference to TextView tv_latitude 
    TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude); 

    // Setting Current Longitude 
    tvLongitude.setText("Longitude:" + location.getLongitude()); 

    // Setting Current Latitude 
    tvLatitude.setText("Latitude:" + location.getLatitude()); 

    value1 = tvLongitude.getText().toString(); 
    value2 = tvLatitude.getText().toString(); 

    // saveClicked(); 

    SaveClicked2(); 




} 

public void SaveClicked2() { 

    try{ 

    File file = new File("/sdcard/Sree.txt"); 
    file.createNewFile(); 
    FileOutputStream fOut = new FileOutputStream(file); 
    OutputStreamWriter out = new OutputStreamWriter(fOut); 
    out.append(value1); 
    out.append(value2); 
    out.close(); 

    Toast.makeText(getBaseContext(), 
       "Done writing values to textfile", 
       Toast.LENGTH_SHORT).show(); 

    } 
    catch(Exception e){ 

     Toast.makeText(getBaseContext(), e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 



    } 







} 

private void saveClicked() { 



    try{ 
     OutputStreamWriter out= 
       new OutputStreamWriter(openFileOutput(STORETEXT, 0)); 
     out.write(value1); 
     out.write(value2); 
     out.close(); 

     Toast 
    .makeText(this, value1, Toast.LENGTH_LONG) 
    .show(); 

    } 
    catch(Throwable t){ 
     Toast.makeText(this, "Exception: "+ t.toString(), Toast.LENGTH_LONG) 
     .show(); 

    } 
} 

@Override 
public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub  
} 

@Override 
public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub  
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    // TODO Auto-generated method stub  
}} 
+0

對不起,我不要我不明白你的問題。通常,客戶端應用程序將文件發送到在服務器上運行的程序,該程序保存該文件。客戶端應用程序將信號發送到服務器上的應用程序,並進行保存/編輯。我不確定它可以做任何不同的事情。我可能是錯的 – baash05

+0

請告訴我如何創建一個接受我的數據,來自客戶端文本文件 – srikanth

回答

1

如果你有SD卡上的文本文件,那麼你可以使用下面的上傳方式將文件上傳到服務器。

您將需要一個腳本,比如這個叫uploader.php

<?php 

    $target_path = "H:/www/yourwebsitedirectory.com/"; 
    $target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['uploadedfile']['name']). 
    " has been uploaded"; 
    } else{ 
    echo "There was an error uploading the file, please try again!"; 
    } 

    ?> 

然後在你的java你可以有一個上傳功能,像這樣的PHP腳本: 請務必填寫您的適當的異常處理,你需要抓取HTTPClient庫。

 public static void Uploader(File fname, String fpath) { 
      try { 
      DefaultHttpClient httpclient = new DefaultHttpClient(); 
      httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

      String postURL = "http://www.yourwebsite.com/uploader.php"; 
      HttpPost httppost = new HttpPost(postURL); 

      // the boundary key below is arbitrary, it just needs to match the MPE so it can decode multipart correctly 
      httppost.setHeader("Content-Type", "multipart/form-data; boundary=--32530126183148"); 
      MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--32530126183148", Charset.forName("UTF-8")); 
    try { 
     mpEntity.addPart("uploadedfile", new FileBody((fname), "application/txt")); 
     mpEntity.addPart("MAX_FILE_SIZE", new StringBody("100000")); 

    } catch (Exception e1) { 
    } 
      httppost.setEntity(mpEntity); 
      HttpResponse response; 
      try { 
       response = httpclient.execute(httppost); 
       HttpEntity resEntity = response.getEntity(); 
       if (resEntity != null) { 
        resEntity.consumeContent(); 
       } 
      } catch (ClientProtocolException e) { 
      } catch (Exception e) { 
      } 
      httpclient.getConnectionManager().shutdown(); 
      } catch (Throwable e) { 
       try { 
       } catch (Throwable e1) { 
       } 
      } 
     } 

調用該函數是這樣的:

File tmpDir = new File(android.os.Environment.getExternalStorageDirectory(),"Directory of your file"); 
    File fname = new File(tmpDir, filesnameonSD); 
    Uploader(fname); 

,並測試你的PHP腳本,你可以使用一個網頁有以下簡單的HTML表單

<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
    Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
    <input type="submit" value="Upload File" /> 
    </form> 
1

我認爲你需要做的是建立一個服務器端應用程序(即Web服務),將接受來自客戶端的Android應用程序中的數據,並在服務器上創建的文件。

你不直接從Android應用程序訪問服務器上的文件系統 - 你只是將信息發送到服務器應用程序來處理。

有可用的很多教程在線創建web服務。

+0

請給我一個樣本示例或樣本的鏈接,可以讓我的數據以文本文件存儲。 – srikanth

+0

這是最簡單的部分,可以使用java File class來完成 - 您可以通過它來獲取數百個示例。你可能應該擔心的是首先創建服務器端應用程序或web服務來接受來自android應用程序的數據。然後你可以將這些數據保存在服務器上。 – Zyga

相關問題