<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_name"
android:hint="请输入用户名"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_main_pwd"
android:hint="请输入用户名"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录GET"
android:onClick="getdata"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录POST"
android:onClick="postdata"
/>
public class MainActivity extends AppCompatActivity {
private EditText et_main_name;
private EditText et_main_pwd;
private HttpURLConnection httpURLConnection;
private URL url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_name = (EditText) findViewById(R.id.et_main_name);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
}
//获取GET提交数据的点击事件
public void getdata(View view){
String name=et_main_name.getText().toString();
String pwds=et_main_pwd.getText().toString();
String path="http://192.168.43.143:8080/login/login.xhtml";
new myTask().execute(name,pwds,path,"GET");
}
//写一个异步任务类继承AsyncTask,重写它的两个方法
class myTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//获取参数的值
String name = params[0].toString();
String pwds = params[1].toString();
String path = params[2].toString();
String type = params[3].toString();
String s = "uname=" + name + "&pwd=" + pwds;
//判断提交数据的类型1、get 2、post
try {
if ("GET".equals(type)) {//用get方式提交
path = path + "?" + s;
url = new URL(path);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
} else if ("POST".equals(type)) {//用post方式提交
}
httpURLConnection.setConnectTimeout(5000);
if (httpURLConnection.getResponseCode() == 200) {
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String result = bufferedReader.readLine();
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
String str= (String) o;
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}
}
public class MainActivity extends AppCompatActivity {
private EditText et_main_name;
private EditText et_main_pwd;
private HttpURLConnection httpURLConnection;
private URL url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_name = (EditText) findViewById(R.id.et_main_name);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
}
//获取POST提交数据的点击事件
public void postdata(View view){
String name=et_main_name.getText().toString();
String pwds=et_main_pwd.getText().toString();
String path="http://192.168.43.143:8080/login/login.xhtml";
new myTask().execute(name,pwds,path,"POST");
}
//写一个异步任务类继承AsyncTask,重写它的两个方法
class myTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
//获取参数的值
String name = params[0].toString();
String pwds = params[1].toString();
String path = params[2].toString();
String type = params[3].toString();
String s = "uname=" + name + "&pwd=" + pwds;
//判断提交数据的类型1、get 2、post
try {
if ("GET".equals(type)) {//用get方式提交
} else if ("POST".equals(type)) {//用post方式提交
url = new URL(path);//得到请求的路径
httpURLConnection = (HttpURLConnection) url.openConnection();
//设置Content-Length Content-Type
httpURLConnection.setRequestProperty("Content-Length", s.length() + "");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置允许对外输出数据
httpURLConnection.setDoOutput(true);
//将用户名密码以流的形式对外输出
httpURLConnection.getOutputStream().write(s.getBytes());
httpURLConnection.setRequestMethod("POST");//数据提交的类型
}
httpURLConnection.setConnectTimeout(5000);
if (httpURLConnection.getResponseCode() == 200) {//判断响应码
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String result = bufferedReader.readLine();
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
String str= (String) o;
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}
}
<uses-permission android:name="android.permission.INTERNET" />
//设置Content-Length Content-Type
httpURLConnection.setRequestProperty("Content-Length", s.length() + "");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有