我已经在android上开发了一个应用程序,它的工作是使用Google Maps API收集地点的gps坐标,然后用户必须将地点的信息输入到坐标并通过php提交到mysql数据库。我的问题是,除了将数据提交到php进行处理外,是否能够做所有事情。是android开发的新手,对您的任何帮助将不胜感激,一个很好的解释让我下一次独奏也很棒。谢谢
PS:一切都在一个文件中,因为我正在使用Samsung 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();
}
}
最佳答案
我不是Java或Android方面的专家,所以请多多包涵...
static final String MOTHERSHIP = "[MyServerAddress/server.php";
不知道这只是一个错字还是什么...但是您要在此前面加上
[
,并且这不应该是提交给HttPClient的绝对http://
路径吗?另外,您还必须在清单文件中添加权限...
<uses-permission android:name="android.permission.INTERNET" />
另外...在您的
connect()
方法中...您正在设定
String result = null;
然后在底部调用它...
alert(result);
return result;
但是我在代码中什么地方都看不到您在使用它,所以它不会总是
null
吗???
关于php - Android数据到PHP到MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17263491/