package com.example.a350773523.myapplication; import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class MainActivity extends AppCompatActivity { private Button btnLogin;
private EditText etLoginID;
private String address = "http://10.12.75.3:8080/testServer/api.jsp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etLoginID = (EditText) findViewById(R.id.etLoginID); //get方式进行联网交流
/*
findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(etLoginID.getText().toString())){
Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_LONG).show();
}
String LoginID;
LoginID = etLoginID.getText().toString();
new AsyncTask<String,Void,Void>(){
@Override
protected Void doInBackground(String... strings) {
try {
URL url =new URL(strings[0]);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String line ;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
isr.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(address+("?loginID=")+LoginID);
}
});
*/ //POST方法
/*
findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(etLoginID.getText().toString())){
Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_LONG).show();
}
final String LoginID;
LoginID = etLoginID.getText().toString();
new AsyncTask<String,Void,Void>(){ //异步任务防止阻塞主线程
@Override
protected Void doInBackground(String... strings) {
try {
URL url =new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true);
connection.setRequestMethod("POST"); //上传信息
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(),"utf-8");
BufferedWriter bw =new BufferedWriter(osw);
bw.write(String.format("%s%s", "loginID=", LoginID));
bw.flush(); //接受信息
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String line ;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
isr.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(address);
}
});
*/
}
}
xml布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.a350773523.myapplication.MainActivity"> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LoginID" /> <EditText
android:id="@+id/etLoginID"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</LinearLayout> <Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login" /> </LinearLayout>