简体   繁体   中英

inserting data to wamp server from android

i am trying to enter data to my database through wamp server from android. since some parameters like namevaluepair or http client are not working in newer api version of android, i am using target version 21 of api. i am trying to log the coordinates to the database,but whatever value may it be an integer is not getting stored in it.though i am getting message of "data has been successfuly updated".please help me through these. here are my php and java file.

php file-

<?php

$con=mysqli_connect('localhost ','root','');
mysqli_select_db("tracer1",$con);

$latlong=$_POST['latlong'];


mysqli_query("insert into tracecoordinate2(latlong)values('{$latlong}')");

?>

HERE IS MY JAVA FILE FROMM ANDROID

package aditya.com.databaseintegration;

import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static aditya.com.databaseintegration.R.id.textView;

/**
 * Code to demonstrate the insertion of data into android application.
 * Also demonstrates how to retrieve the data.
 * Uses the localhost for local server.
 * Php scripting language for backend integration.
 */

    public class MainActivity extends ActionBarActivity {

    EditText etlatlong;
    private Button b;

    Button bSubmit;
    private LocationManager locationManager;
    private LocationListener listener;
    public static String url = "http://10.0.2.2/tracer.php";
    String latlong;
    InputStream is = null;
    String exceptionMessage = "There seems to be some problem connecting to database. " +
            "Please check your Internet Connection and try again.";
    String successMessage = "Data has been entered successfully";

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

        StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(threadPolicy);

        etlatlong = (EditText) findViewById(textView);

        bSubmit = (Button) findViewById(R.id.button2);
        b = (Button) findViewById(R.id.button);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                etlatlong.append("\n " + location.getLongitude() + " " + location.getLatitude());
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }
        };

        configure_button();


        bSubmit.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {

                                          String latlong= etlatlong.getText().toString();


                                            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(1);
                                           nameValuePairList.add(new BasicNameValuePair("latlong", latlong));


                                           try {
                                               HttpClient httpClient = new DefaultHttpClient();
                                               HttpPost httpPost = new HttpPost(url);
                                               httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));
                                               HttpResponse httpResponse = httpClient.execute(httpPost);
                                               HttpEntity httpEntity = httpResponse.getEntity();
                                               is = httpEntity.getContent();
                                               etlatlong.setText("");

                                               Toast.makeText(getApplicationContext(), successMessage, Toast.LENGTH_SHORT).show();
                                               is.close();
                                           } catch (IOException e) {
                                               Toast.makeText(getApplicationContext(), exceptionMessage, Toast.LENGTH_SHORT).show();
                                           }
                                       }
                                   }
        );
    }



    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 10:
                configure_button();
                break;
            default:
                break;
        }
    }


    void configure_button(){
        // first check for permissions

        // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //noinspection MissingPermission
                locationManager.requestLocationUpdates("gps", 5000, 0, listener);
            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        else if( id == R.id.action_retrieve_data){
            Intent it = new Intent(MainActivity.this, RetrieveData.class);
            startActivity(it);
        }

        return super.onOptionsItemSelected(item);
    }
}

thank you :) i finally figured out when i tried to send post through POSTMAN (chrome web store) where data was not being stored. but then the problem i figured was the whole folder of database needed to be stored in www folder (c/wamp64/www)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM