java - 将 Android 应用程序中的数据插入到 mySql 数据库中

标签 java android insert

我一直在关注 YouTube 视频,该视频解释了如何将数据从 Android 应用程序插入到 mySQL。 它对我不起作用,我不知道我做错了什么。我一执行它就中断了。甚至不加载 textViews 和 Buttons

这是我的主要 Activity :

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();
                }

            }
        });


    }


}

这是我的 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>

这是我的 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>

最佳答案

我猜您收到 ClassCastException 是因为您试图调用 (EditText)TextView上.

更改此:

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);

或者将您的 xml 标记从 TextView 更改为 EditText。

<EditText />

注意:别人指出的也是正确的。如果您点击提交按钮,您将得到

android.os.NetworkOnMainThreadException

所以你需要使用 HandlerAsyncTask

关于java - 将 Android 应用程序中的数据插入到 mySql 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27271286/

相关文章:

java - Weblogic动态类加载

java - '(' , ' )', <column constraint> or comma expected, got ' TEXT' android studio 中的 SQLite 数据库错误

java - 如何初始化已声明的变量?

java - 将其他证书添加到现有 jks 或不使用 javax.net.ssl.trustStore 进行请求

java - 通过 JNI 从 Java 代码调用 .net DLL 未返回

android - 清除自定义适配器

Android 控制 TableLayout 中选中的复选框

sql - 从一个表中选择,插入到另一个表中oracle sql query

php - 如何在addform中使用可选字段?

sql-server - 将 Access 查询中的 Bool 插入 SQL Server 数据库