package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private Button but;
private EditText edname;
private EditText edpwd;
private String pwd;
private String name;
private TextView text;
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.but);
edname = (EditText) findViewById(R.id.edname);
edpwd = (EditText) findViewById(R.id.edpwd);
text = (TextView) findViewById(R.id.text);
//通过点击实现上传数据 并且回传
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(){
@Override
public void run() {
name = edname.getText().toString();
pwd = edpwd.getText().toString();
getdata(name,pwd);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
text .setText(s);
}
});
}
}.start();
}
});
}
//获取网络资源的数据
private void getdata(String name,String pwd){
String path = "http://123.206.70.44:8080/JavaWebTest/Upload_html?";
try {
URL url = new URL(path);
HttpURLConnection HttpURLConnection = (HttpURLConnection) url.openConnection();
HttpURLConnection.setRequestMethod("POST");
HttpURLConnection.setReadTimeout(1024*8);
HttpURLConnection.setConnectTimeout(1024*8);
//指定要给服务器写数据
HttpURLConnection.setDoInput(true);
String data = "user="+name+"&password="+pwd;
HttpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
HttpURLConnection.setRequestProperty("Content-Length",data.length()+"");
//开始向服务器写数据
HttpURLConnection.getOutputStream().write(data.getBytes());
int code = HttpURLConnection.getResponseCode();
//设置响应的类型 和长度
if (code==200){
Log.d("wwb",code+"");
InputStream inputStream = HttpURLConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[]buf = new byte[1024*8];
while((len= inputStream.read(buf))!=-1){
baos.write(buf,0,len);
s = baos.toString();
}
}else{
Log.d("wwb",code+"");
}
} catch (Exception e) {
e.printStackTrace();
}
}
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edname"
android:hint="请输入账号"
android:layout_margin="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edpwd"
android:hint="请输入密码"
android:password="true"
android:inputType="number"
android:layout_margin="20dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:id="@+id/but"
android:layout_margin="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text"/>
</LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private Button but;
private EditText edname;
private EditText edpwd;
private String pwd;
private String name;
private TextView text;
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.but);
edname = (EditText) findViewById(R.id.edname);
edpwd = (EditText) findViewById(R.id.edpwd);
text = (TextView) findViewById(R.id.text);
//通过点击实现上传数据 并且回传
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread(){
@Override
public void run() {
name = edname.getText().toString();
pwd = edpwd.getText().toString();
getdata(name,pwd);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
text .setText(s);
}
});
}
}.start();
}
});
}
//获取网络资源的数据
private void getdata(String name,String pwd){
String path = "http://123.206.70.44:8080/JavaWebTest/Upload_html?";
try {
URL url = new URL(path);
HttpURLConnection HttpURLConnection = (HttpURLConnection) url.openConnection();
HttpURLConnection.setRequestMethod("POST");
HttpURLConnection.setReadTimeout(1024*8);
HttpURLConnection.setConnectTimeout(1024*8);
//指定要给服务器写数据
HttpURLConnection.setDoInput(true);
String data = "user="+name+"&password="+pwd;
HttpURLConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
HttpURLConnection.setRequestProperty("Content-Length",data.length()+"");
//开始向服务器写数据
HttpURLConnection.getOutputStream().write(data.getBytes());
int code = HttpURLConnection.getResponseCode();
//设置响应的类型 和长度
if (code==200){
Log.d("wwb",code+"");
InputStream inputStream = HttpURLConnection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[]buf = new byte[1024*8];
while((len= inputStream.read(buf))!=-1){
baos.write(buf,0,len);
s = baos.toString();
}
}else{
Log.d("wwb",code+"");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" android:orientation="vertical" tools:context="com.example.dell.wujin0711.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edname"
android:hint="请输入账号"
android:layout_margin="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edpwd"
android:hint="请输入密码"
android:password="true"
android:inputType="number"
android:layout_margin="20dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:id="@+id/but"
android:layout_margin="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text"/>
</LinearLayout>
//网络请求
<uses-permission android:name="android.permission.INTERNET"></uses-permission>