Java 将android应用程序中的数据插入mySql数据库

Java 将android应用程序中的数据插入mySql数据库,java,android,insert,Java,Android,Insert,我一直在关注一段youtube视频,该视频解释了如何将安卓应用程序中的数据插入mySQL。 这对我不起作用,我不知道我做错了什么。我一执行它,它就坏了。甚至没有加载文本视图和按钮 这是我的主要活动: package com.example.insertbbdd; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import or

我一直在关注一段youtube视频,该视频解释了如何将安卓应用程序中的数据插入mySQL。 这对我不起作用,我不知道我做错了什么。我一执行它,它就坏了。甚至没有加载文本视图和按钮

这是我的主要活动:

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

            }
        });


    }


}
package com.example.insertbbdd;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.support.v4.app.Fragment;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
EditText-eName、eAge、电子邮件;
按钮bSumbit;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_活动);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
eName=(EditText)findViewById(R.id.editName);
eAge=(编辑文本)findViewById(R.id.editAge);
eMail=(EditText)findViewById(R.id.editMail);
bSumbit=(按钮)findViewById(R.id.submitButton);
bSumbit.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
InputStream=null;
字符串名称=”“+eName.getText().toString();
字符串age=”“+eAge.getText().toString();
字符串email=“”+email.getText().toString();
List nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“年龄”,年龄));
添加(新的BasicNameValuePair(“email”,email));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://accessibility.es/prueba/script.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
is=entity.getContent();
String msg=“数据输入成功”;
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG.show();
}
捕获(客户端协议例外e){
Log.e(“客户端协议”、“日志标签”);
e、 printStackTrace();
}
捕获(IOE异常){
Log.e(“日志标签”、“IOException”);
e、 printStackTrace();
}
}
});
}
}
这是我的主要_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>

您需要在主UI线程之外的单独线程中执行任何网络操作,如HttpPost(或任何其他慢速操作),然后使用回调将UI更新回主线程


有关主要文档,请参阅和。网络上也有很多指南,例如
文本视图
上的
(编辑文本)
,我猜你会得到ClassCastException

更改此项:

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 />

NB:其他人指出的也是正确的。如果单击“提交”按钮,您将获得

android.os.NetworkOnMainThreadException

因此,您需要使用或中的任何一个,请尝试此

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

在UI线程上无法执行网络任务的可能重复项。