app提交信息到PC端mysql数据库
新建名为SignActivity
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();
} }
HttpThread
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.xml
<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了