您好我在文本文件中存储经度和纬度值并将它们保存在我的sdcard.now我想将此数据保存到webserver(java)textfile.please告诉我如何在服务器中创建文本文件以及如何发布数据到那个文件。这是我的代码。
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 :(得分:1)
我认为你需要做的是创建一个服务器端应用程序(即webservice),它将接受来自客户端Android应用程序的数据并在服务器上创建文件。
您不能直接从Android应用程序访问服务器文件系统 - 您只需将信息发送到服务器应用程序即可处理。
网上有很多可用于创建网络服务的教程。
答案 1 :(得分:1)
如果SD卡上有文本文件,则可以使用以下上传方法将文件上传到服务器。
您将需要一个脚本,例如名为uploader.php的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中你可以拥有一个像这样的Uploader函数: 确保填写正确的异常处理,您需要获取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>