首页 技术 正文
技术 2022年11月17日
0 收藏 520 点赞 3,325 浏览 3823 个字
 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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291