Data from Android App -> MYSQL
I'm developing an android application which is to collect data and then send it to a web directory.
So lets say a want to collect an array of data on the phone, and then after clicking a button send it all to the online directory as a file or stream. It does not even need to get a response - although in the future a confirmation would be handy.
how to be send to data to mysql
i submit my code as follow
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
InputStream inputStream;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nameValuePairs.add(new BasicNameValuePair("Lat","19.80"));
nameValuePairs.add(new BasicNameValuePair("Lon","13.22"));
try{
开发者_开发百科 HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/guestfeedback/connection.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
}catch (Exception e) {
// TODO: handle exception
}
}
I would recommend an IntentService and HttpPost, personally. The documentation on both of them is pretty easy to follow.
精彩评论