app提交信息到PC端mysql數據庫java
新建名爲SignActivitymysql
package com.example.administrator.success; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SignActivity extends Activity { private EditText name; // private EditText password; private Button signup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign); name=(EditText) findViewById(R.id.etSgAccount); // password=(EditText) findViewById(R.id.etSgPassword); signup=(Button) findViewById(R.id.btnSign); } /*發起HTTP請求*/ public void onLogin(View v) { String url="http://182.92.66.60:8083/saveTestApp.jsp"; new HttpThread(url, name.getText().toString()).start(); } }
HttpThreadandroid
package com.example.administrator.success; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.R.string; public class HttpThread extends Thread { String url; String name; // String password; public HttpThread(String url, String name) { // TODO Auto-generated constructor stub this.url = url; this.name = name; // this.password = password; } private void doGet() throws IOException { /*將username和password傳給Tomcat服務器*/ url=url+"?name="+name; try { URL httpUrl = new URL(url); /*獲取網絡鏈接*/ HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); /*設置請求方法爲GET方法*/ conn.setRequestMethod("GET"); /*設置訪問超時時間*/ conn.setReadTimeout(5000); BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; StringBuffer sb=new StringBuffer(); //讀取服務器返回的信息 while((str=reader.readLine())!=null) { sb.append(str); } //把服務端返回的數據打印出來 System.out.println("result"+sb.toString()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /*在run中調用doGet*/ @Override public void run() { try { doGet(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
佈局文件activity_sign.xmlsql
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusableInTouchMode="true" android:background="#ff6699cc" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fffffffb" android:orientation="horizontal" android:padding="40dp" > <EditText android:id="@+id/etSgAccount" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginBottom="20dp" android:hint="Account" android:inputType="textEmailAddress" /> <EditText android:id="@+id/etSgPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/etSgAccount" android:layout_marginBottom="20dp" android:hint="Password" android:inputType="textPassword" /> <EditText android:id="@+id/etSgRePassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/etSgPassword" android:layout_marginBottom="20dp" android:hint="repassword" android:inputType="textPassword" /> <Button android:id="@+id/btnSign" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/etSgRePassword" android:background="#ff6699cc" android:onClick="onLogin" android:text="signup" android:textColor="#ffffffff" android:textSize="24sp" /> </RelativeLayout> </LinearLayout>
本身搭一個Tomact服務器,把SignActivity裏的url換成本身的就OK了數據庫