1
我已經開發了一個應用程序在Android上,它是做什麼是收集使用谷歌地圖API的地方的GPS座標用戶將不得不輸入地方的信息,並提交給一個通過php的mysql數據庫。我的問題是,除了將數據提交給php來處理結果之外,我能夠做所有事情。對於android開發來說是新的,任何幫助都將不勝感激,下一次單獨演示的一個很好的解釋將會非常棒。謝謝Android數據以PHP的MYSQL
PS:一切都在一個文件因爲我在我的三星Galaxy選項卡上使用AIDE開發,基本版本將不允許外部文件。
package com.gpsproject.stgps;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.location.*;
import java.io.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.text.*;
import javax.xml.transform.*;
public class MainActivity extends Activity
{
private Button btnsave;
private EditText bizname;
private EditText bizaddr;
private EditText bizphone;
private EditText bizemail;
private EditText bizwebsite;
private EditText bizarea;
private EditText bizdescription;
private TextView txtinfo;
Location location;
String params;
String errmsg;
Boolean saveData = false;
static final String MOTHERSHIP = "[MyServerAddress/server.php";
LocationListener myLocationListener = new LocationListener() {
public void onLocationChanged(Location _location) {
String msg;
msg = String.format("Latitude: %s \nLongitude: %s\nAltitude: %s\nAccuracy: %s m", _location.getLatitude(),
_location.getLongitude(), _location.getAltitude(), _location.getAccuracy());
txtinfo.setText(msg);
location = _location;
saveData = true;
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
String msg;
msg = String.format("provider %s status %d Extras %s", provider, status, extras);
Toast notify = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
notify.setGravity(Gravity.TOP, 0, 0);
notify.show();
}
public void onProviderEnabled(String provider)
{
String msg;
msg = String.format("provider %s ENABLED", provider);
Toast notify = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
notify.setGravity(Gravity.TOP, 0, 0);
notify.show();
}
public void onProviderDisabled(String provider)
{
String msg;
msg = String.format("provider %s DISABLED", provider);
Toast notify = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
notify.setGravity(Gravity.TOP, 0, 0);
notify.show();
}
};
public String connect(String url)
{
String result = null;
try {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
response = httpclient.execute(httpget);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
alert(String.format("entity for outside... %s", entity));
if (entity != null) {
alert(String.format("Entity too dey fine fine.....", entity));
InputStream instream = entity.getContent();
result= convertStreamToString(instream);
// now you have the string representation of the HTML request
instream.close();
} else {
alert(String.format("i no dey be: %s", entity));
}
} catch (Exception e) {
//errmsg = e.getMessage();
alert(e.getMessage());
}
alert(result);
return result;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtinfo = (TextView)findViewById(R.id.txtvLatlon);
bizname = (EditText)findViewById(R.id.txtBizname);
bizaddr = (EditText)findViewById(R.id.txtAddress);
bizphone = (EditText)findViewById(R.id.txtPhone);
bizemail = (EditText)findViewById(R.id.txtEmail);
bizwebsite = (EditText)findViewById(R.id.txtWebsite);
bizarea = (EditText)findViewById(R.id.txtArea);
bizdescription = (EditText)findViewById(R.id.txtDescription);
btnsave = (Button)findViewById(R.id.btnSave);
btnsave.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
String ret;
String req;
req = String.format("bizname=%s&address=%s&phone=%s&email=%s&website=%s&area=%s&description=%s&latitude=%s&longitude=%s&accuracy=%s",
convertURL(bizname.getText().toString()), convertURL(bizaddr.getText().toString()), bizphone.getText(), bizemail.getText(), bizwebsite.getText(),
convertURL(bizarea.getText().toString()), convertURL(bizdescription.getText().toString()),
location.getLatitude(), location.getLongitude(), location.getAccuracy());
req = String.format("%s?%s", MOTHERSHIP, req);
alert(String.format("2 request is::::: ", req));
try {
ret = connect(req);
if (ret != null) {
alert(ret);
} else {
alert(String.format("Error connecting to server: %s", errmsg));
alert(ret);
}
} catch(Exception e) {
alert(e.getMessage());
}
}
});
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocationListener);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, myLocationListener);
}
/**
* Convert inputStream
*
* @param is
* @return String
*/
private String convertStreamToString(InputStream is)
{
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public String convertURL(String str) {
String url = null;
try{
url = new String(str.trim().replace(" ", "%20").replace("&", "%26") .replace(",", "%2c").replace("(", "%28").replace(")", "%29") .replace("!", "%21").replace("=", "%3D").replace("<", "%3C") .replace(">", "%3E").replace("#", "%23").replace("$", "%24") .replace("'", "%27").replace("*", "%2A").replace("-", "%2D") .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A") .replace(";", "%3B").replace("?", "%3F").replace("@", "%40") .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D") .replace("_", "%5F").replace("`", "%60").replace("{", "%7B") .replace("|", "%7C").replace("}", "%7D")); }catch(Exception e){ e.printStackTrace(); } return url; }
private void alert(String msg)
{
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
}
KyleK,是它的錯別字。它的一個完整的網址,但我不想分享它,這就是爲什麼我這樣做,我打算這麼做「[MyServerAddress] ...以指示地址包含在內 但我沒有包括權限或不是我知道了,我會檢查並儘快回覆,謝謝 – mw509
對不起,我保持了這麼長時間的回覆,我已經有了我的XML的權限 \t \t \t <使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> –
mw509