安卓开发--HttpDemo02
2024-10-01 15:49:58
package com.cnn.httpdemo02; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity {
private EditText txtLoginName;
private EditText txtLoginpwd;
private Button btnLogin;
private String httpURL; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpURL="http://192.168.0.106:8088/Api/User/PostUserInfo";
txtLoginName = (EditText) findViewById(R.id.txtLoginName);
txtLoginpwd = (EditText) findViewById(R.id.txtLoginPwd);
btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
new UserThread(txtLoginName.getText().toString(), txtLoginpwd.getText().toString(), httpURL).start();
}
});
} }
package com.cnn.httpdemo02; import java.io.BufferedReader;
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.URLDecoder;
import java.net.URLEncoder; import android.util.Log; public class UserThread extends Thread {
private String LoginName,LoginPwd,HttpURL; public UserThread(String LoginName,String LoginPwd, String HttpURL){
this.LoginName=LoginName;
this.LoginPwd=LoginPwd;
this.HttpURL=HttpURL;
} private void GetUserInfo(){
try {
HttpURL=HttpURL+"?loginname="+URLEncoder.encode(LoginName,"utf-8")+"&loginpwd="+LoginPwd;
URL httpURL=new URL(HttpURL);
HttpURLConnection conn=(HttpURLConnection) httpURL.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String str;
StringBuffer buffer=new StringBuffer();
while ((str=reader.readLine())!=null) {
buffer.append(str);
//Log.i("测试", str);
} //System.out.print(buffer.toString());
Log.i("tag", buffer.toString());
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} private void PostUserInfo() {
try {
URL httpURL=new URL(HttpURL);
HttpURLConnection connection=(HttpURLConnection) httpURL.openConnection();
connection.setReadTimeout(5000);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
String content = "loginname="+LoginName+"&loginpwd="+LoginPwd+"&a=29";
outputStream.write(content.getBytes()); BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String str;
StringBuffer buffer=new StringBuffer();
while ((str=reader.readLine())!=null) {
buffer.append(str);
} Log.i("tag", buffer.toString());
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
} @Override
public void run() {
// TODO 自动生成的方法存根
//GetUserInfo();
PostUserInfo();
}
}
<RelativeLayout 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: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="com.cnn.httpdemo02.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_marginTop="15dp"
android:text="用户名" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="密码" /> <EditText
android:id="@+id/txtLoginName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="34dp"
android:layout_toRightOf="@+id/textView1"
android:hint="请输入您的用户名"
android:ems="10" > <requestFocus />
</EditText> <EditText
android:id="@+id/txtLoginPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="34dp"
android:layout_toRightOf="@+id/textView2"
android:hint="请输入您的密码"
android:ems="10" > <requestFocus />
</EditText> <Button
android:id="@+id/btnLogin"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtLoginPwd"
android:layout_below="@+id/txtLoginPwd"
android:layout_marginTop="56dp"
android:text="登陆" /> </RelativeLayout>
最新文章
- QT打开ROS工作空间时遇到的问题和解决方法
- Linux命令总结:file
- Windows x86/ x64 Ring3层注入Dll总结
- C# JabLib系列之如何保证只运行一个应用程序的实现
- JQuery 动态添加onclick事件
- 《iOS开发指南》正式出版-源码-样章-目录,欢迎大家提出宝贵意见
- [React Native] Complete the Notes view
- an error occured during the file system check
- iOS 饼状图
- Week11(11月18日)
- tflearn save模型异常
- jvm参数解析(含调优过程)
- Magicodes.Admin.Core开源框架总体介绍
- mysql压缩包安装方式
- 【转】cookielib模块
- truncated、delete和drop的异同点
- ECMAScript简介以及es6新增语法
- HDU 4400 Mines(好题!分两次计算距离)
- python短域名数据分析框架
- Windows 安装 setuptools 和 feedparser
热门文章
- 海尔公布首套智能套装SmartCare,主打智能、简单
- GNU Linux中的SO_RCVLOWAT和SO_SNDLOWAT说明
- javaScript常用知识点有哪些
- hdoj--1016--Prime Ring Problem(递归回溯)
- Service Mesh(服务网格)
- 【DNN】 安装问题
- e.Row.Attributes.Add
- Golang环境配置Centos
- DotNetCore.1.0.1-VS2015Tools.Preview2.0.3 相关问题及解决办法
- jq 鼠标点击跳转页面后 改变点击菜单的样式代码