0

I've been following a youtube video which explain how to insert data from an android app to mySQL. It doesn't work for me and I don't know what am I doing wrong. It just breaks as soon as I execute it. Not even loading the textViews and Buttons

This is my MainActivity:

package com.example.insertbbdd;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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 android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText eName, eAge, eMail;
    Button bSumbit;

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

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

        eName = (EditText) findViewById(R.id.editName);
        eAge = (EditText) findViewById(R.id.editAge);
        eMail = (EditText) findViewById(R.id.editMail);
        bSumbit = (Button) findViewById(R.id.submitButton);

        bSumbit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                InputStream is = null;

                String name = "" + eName.getText().toString();
                String age = "" + eAge.getText().toString();
                String email = "" + eMail.getText().toString();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

                nameValuePairs.add(new BasicNameValuePair("name", name));
                nameValuePairs.add(new BasicNameValuePair("age", age));
                nameValuePairs.add(new BasicNameValuePair("email", email));


                try{
                    HttpClient httpClient = new DefaultHttpClient();

                    HttpPost httpPost = new HttpPost("http://accessibility.es/prueba/script.php");

                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                    HttpResponse response = httpClient.execute(httpPost);

                    HttpEntity entity = response.getEntity();

                    is = entity.getContent();

                    String msg = "Data entered succesfully";
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

                }
                catch(ClientProtocolException e){
                    Log.e("Client Protocol", "Log_tag");
                    e.printStackTrace();
                }
                catch(IOException e){
                    Log.e("Log tag", "IOException");
                    e.printStackTrace();
                }

            }
        });


    }


}

This is my main_activity.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/submitButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editName"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="130dp"
        android:text="Button" />

    <TextView
        android:id="@+id/editMail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/submitButton"
        android:layout_alignRight="@+id/editName"
        android:layout_marginBottom="54dp"
        android:layout_marginRight="17dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/editAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editMail"
        android:layout_alignLeft="@+id/editMail"
        android:layout_marginBottom="43dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/editName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editAge"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="35dp"
        android:layout_marginLeft="90dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

And this is my AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.insertbbdd"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 1
    possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – M D Dec 03 '14 at 12:04

3 Answers3

2

I'm guessing you are getting the ClassCastException because you are trying to call (EditText) on a TextView.

change this :

TextView eName, eAge, eMail;

eName = (TextView) findViewById(R.id.editName);
eAge = (TextView) findViewById(R.id.editAge);
eMail = (TextView) findViewById(R.id.editMail);
bSumbit = (Button) findViewById(R.id.submitButton);

or change your xml tag from TextView to EditText.

<EditText />

NB: what is pointed by others is also correct. if you click your submit button you will get

android.os.NetworkOnMainThreadException

so you need to use either Handler or AsyncTask

0

You need to carry out any network operations such as HttpPost (or indeed any other slow operation) in a separate thread from the main UI thread, and then use a callback to update the UI back on the main thread later.

See http://developer.android.com/guide/components/processes-and-threads.html and http://developer.android.com/reference/android/os/AsyncTask.html for the main documentation. There are also numerous guides on the web eg http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

0

try this..

try{
                new Thread(new Runnable() {
            public void run() {

               HttpClient httpClient = new DefaultHttpClient();

                HttpPost httpPost = new HttpPost("http://accessibility.es/prueba/script.php");

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpClient.execute(httpPost);

                HttpEntity entity = response.getEntity();

                is = entity.getContent();

                String msg = "Data entered succesfully";
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();

            }
            catch(ClientProtocolException e){
                Log.e("Client Protocol", "Log_tag");
                e.printStackTrace();
            }
            catch(IOException e){
                Log.e("Log tag", "IOException");
                e.printStackTrace();
            }

                }
        }).start();
H4SN
  • 1,482
  • 3
  • 24
  • 43