连接数据库和插入数据

时间:2016-04-02 05:22:23

标签: android

MainActivty.java

package com.data.database;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
Button b;
EditText e1;
String response="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    b=(Button)findViewById(R.id.button);
    e1=(EditText)findViewById(R.id.namee);

}
public void SUBMIT(View view){
String name=e1.getText().toString();
String method="register";
BackgroudTask backgroudTask=new BackgroudTask(this);
backgroudTask.execute(method,name);

}

} 

BackgroudTask.java

package com.data.database;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class BackgroudTask extends AsyncTask<String,Void,String> {
Context ctx;
BackgroudTask(Context ctx){
    this.ctx=ctx;
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
}


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

    String link="http://comeon.byethost14.com/name.php";
    String method=params[0];
    if(method.equals("register")) {
        String name = params[1];
        try {
            URL url = new URL(link);
            HttpURLConnection httpURLConnection=     (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream os=httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter=new BufferedWriter(new  OutputStreamWriter(os,"UTF-8"));
            String data= URLEncoder.encode("NAME","UTF- 8")+"="+URLEncoder.encode(name,"UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            os.close();
            InputStream is=httpURLConnection.getInputStream();
                    is.close();
            return  "Registration Success";
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}


protected void onProgressUpdate(Void values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
    Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}

AndroidManifest.java

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.data.database">
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

content_main.xml;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.data.database.MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/name_text"
    android:id="@+id/name"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="72dp" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/namee"
    android:layout_alignBottom="@+id/name"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="53dp"
    android:layout_marginEnd="53dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/submit_button"
    android:id="@+id/button"
    android:layout_below="@+id/namee"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="114dp"
    android:onClick="SUBMIT"/>

</RelativeLayout>

这是我的代码。当我在手机中运行应用程序并尝试输入名称并提交它时,我的数据库中没有任何变化。我正在尝试第一次这样做.PHP脚本没问题因为我在浏览器的URL中检查了他们的URL,并收到了正确的输出和消息。我甚至尝试运行php脚本,该脚本应该在数据库中插入数据并且它完美地工作。任何善意的帮助将是很好。谢谢你。另一件事我认为会有所帮助,每次点击按钮提交时都会显示消息“Resgistraion Success”。

0 个答案:

没有答案