mugil
mugil

Reputation: 11

How connect mysql in android

i followed the example on the internet. when i do is working success, but it does not work when i increase the variable. It works well in html and there is no error when i build in my phone. However, the data is not passed to mysql.

Of course I added the internet permission to the manifest

there is example that i followed on internet. it does work well.

public class MainActivity extends Activity {

    private EditText editTextName;
    private EditText editTextAdd;


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

        editTextName = (EditText) findViewById(R.id.name);
        editTextAdd = (EditText) findViewById(R.id.mobile);

    }

    public void insert(View view){
        String name = editTextName.getText().toString();
        String mobile = editTextAdd.getText().toString();

        insertToDatabase(name, mobile);

    }

    private void insertToDatabase(String name, String mobile){

        class InsertData extends AsyncTask<String, Void, String>{
            ProgressDialog loading;


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(MainActivity.this, "Please Wait", null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {

                try{
                    String name = (String)params[0];
                    String mobile = (String)params[1];

                    String link="http://gilmucompany.xyz/gg.php";
                    String data  = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8");
                    data += "&" + URLEncoder.encode("mobile", "UTF-8") + "=" + URLEncoder.encode(mobile, "UTF-8");

                    URL url = new URL(link);
                    URLConnection conn = url.openConnection();

                    conn.setDoOutput(true);
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                    wr.write( data );
                    wr.flush();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    StringBuilder sb = new StringBuilder();
                    String line = null;

                    // Read Server Response
                    while((line = reader.readLine()) != null)
                    {
                        sb.append(line);
                        break;
                    }
                    return sb.toString();
                }
                catch(Exception e){
                    return new String("Exception: " + e.getMessage());
                }

            }
        }

this is problem

public class Insert extends Activity {

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

    }

    public void Insert(View view){

        Intent intent = getIntent();

        String time1 = (String) intent.getStringExtra("time1");
        String time2 = (String) intent.getStringExtra("time2");
        String start = (String) intent.getStringExtra("start");
        String destiny = (String) intent.getStringExtra("destiny");
        String time = (String) intent.getStringExtra("time");


        insertToDatabase(time1,time2,start,destiny,time);
    }

    private void insertToDatabase( String time1, String time2,String start, String destiny, String time){

        class InsertData extends AsyncTask<String, Void, String> {
            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(Insert.this, "Please Wait", null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
            }

            @Override
            protected String doInBackground(String... params) {

                try{

                    String time1 = (String)params[0];
                    String time2 = (String)params[1];
                    String start = (String)params[2];
                    String destiny = (String)params[3];
                    String time = (String)params[4];



                    String link="www.gilmucompany.xyz/insertinformationdata.php/";
                    String data  = URLEncoder.encode("time1", "UTF-8") + "=" + URLEncoder.encode(time1, "UTF-8");
                    data += "&" + URLEncoder.encode("time2", "UTF-8") + "=" + URLEncoder.encode(time2, "UTF-8");
                    data += "&" +URLEncoder.encode("start", "UTF-8") + "=" + URLEncoder.encode(start, "UTF-8");
                    data += "&" + URLEncoder.encode("destiny", "UTF-8") + "=" + URLEncoder.encode(destiny, "UTF-8");
                    data += "&" + URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time, "UTF-8");

                    URL url = new URL(link);
                    URLConnection conn = url.openConnection();

                    conn.setDoOutput(true);
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                    wr.write( data );
                    wr.flush();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    StringBuilder sb = new StringBuilder();
                    String line = null;

                    // Read Server Response
                    while((line = reader.readLine()) != null)
                    {
                        sb.append(line);
                        break;
                    }
                    return sb.toString();
                }
                catch(Exception e){
                    return new String("Exception: " + e.getMessage());
                }

            }
        }

        InsertData task = new InsertData();
        task.execute(time1,time2,start,destiny,time);
    }

Upvotes: 1

Views: 69

Answers (1)

Keerthivasan
Keerthivasan

Reputation: 1661

You forget to add setDoInput(true) before connecting to the server

public class Insert extends Activity {

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

}

public void Insert(View view){

    Intent intent = getIntent();

    String time1 = (String) intent.getStringExtra("time1");
    String time2 = (String) intent.getStringExtra("time2");
    String start = (String) intent.getStringExtra("start");
    String destiny = (String) intent.getStringExtra("destiny");
    String time = (String) intent.getStringExtra("time");


    insertToDatabase(time1,time2,start,destiny,time);
}

private void insertToDatabase( String time1, String time2,String start, String destiny, String time){

    class InsertData extends AsyncTask<String, Void, String> {
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(Insert.this, "Please Wait", null, true, true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }

        @Override
        protected String doInBackground(String... params) {

            try{

                String time1 = (String)params[0];
                String time2 = (String)params[1];
                String start = (String)params[2];
                String destiny = (String)params[3];
                String time = (String)params[4];



                String link="www.gilmucompany.xyz/insertinformationdata.php/";
                String data  = URLEncoder.encode("time1", "UTF-8") + "=" + URLEncoder.encode(time1, "UTF-8");
                data += "&" + URLEncoder.encode("time2", "UTF-8") + "=" + URLEncoder.encode(time2, "UTF-8");
                data += "&" +URLEncoder.encode("start", "UTF-8") + "=" + URLEncoder.encode(start, "UTF-8");
                data += "&" + URLEncoder.encode("destiny", "UTF-8") + "=" + URLEncoder.encode(destiny, "UTF-8");
                data += "&" + URLEncoder.encode("time", "UTF-8") + "=" + URLEncoder.encode(time, "UTF-8");

                URL url = new URL(link);
                URLConnection conn = url.openConnection();

                conn.setDoOutput(true);
                conn.setDoInput(true);  // add this line to receive response from server
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                wr.write( data );
                wr.flush();

                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                StringBuilder sb = new StringBuilder();
                String line = null;

                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    sb.append(line);
                    break;
                }
                return sb.toString();
            }
            catch(Exception e){
                return new String("Exception: " + e.getMessage());
            }

        }
    }

    InsertData task = new InsertData();
    task.execute(time1,time2,start,destiny,time);

Upvotes: 1

Related Questions