安卓开发--HttpDemo02
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>
安卓开发--HttpDemo02的更多相关文章
- 基于eclipse-java的平台上搭建安卓开发环境
首先感谢好人的分享!http://www.mamicode.com/info-detail-516839.html 系统:windows 7 若想直接安装eclipse—android的,请启动如下传 ...
- 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题
刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...
- Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境
我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...
- 安卓开发第一步:Android Studio安装配置
虽然本人是JAVA开发工程师平时主要开发Web App,但因为项目需求需要开发对应的移动端.一时又找不到合适的安卓开发人员,兄弟我只好被项目经理"抓来当壮丁了".俗话说好" ...
- monkeyrunner之安卓开发环境搭建(一)
在学习monkeyrunner之前,让我们先搭建好eclipse安卓开发环境. 对于程序开发人员而言,eclipse并不陌生,它提供了一个非常广阔的平台来开发程序.同样也可以用它来开发android程 ...
- 安卓开发:效果图中标注的像素尺寸如何转换为安卓的dp尺寸?
我们的UI基于1920x1080分辨率给的尺寸标注,但是在安卓开发中大家一般都使用dp.sp来标注界面尺寸,所以需要一个dp与sp的转换公式. 一开始参考的的这篇文章:关于Android开发中px.d ...
- 安卓开发30:AsyncTask的用法
http://blog.csdn.net/initphp/article/details/10392093 安卓开发笔记系列(43) 在开发Android应用时必须遵守单线程模型的原则: Andro ...
- delphi XE5下安卓开发技巧
delphi XE5下安卓开发技巧 一.手机快捷方式显示中文名称 project->options->Version Info-label(改成需要显示的中文名即可),但是需要安装到安卓手 ...
- 推荐 greenrobot eventbus,简化安卓开发,提高安卓维护性,优化安卓性能
最近在研究移动开发,广泛的阅读有关cordova,apicloud,android资料.发现安卓的开发还是很简单的.再发现greenrobot eventbus开源框架不仅可以简化安卓开发,有可以大幅 ...
随机推荐
- video_capture模块分析
1. 对外接口 VideoCaptureModule 控制接口 VideoCaptureDataCallback Vie中的ViECapturer继承,用于响应抓包数据 2 ...
- 如何让Java写的程序,脱离Eclipse在别人的电脑上运行?
Java程序运行,离不开JRE. 将JRE文件夹拷贝出来, 将编写程序的bin文件夹拷贝出来, 再编写一个bat文件. start jre\bin\javaw.exe -cp .\bin\ -Djav ...
- ES设置查询的相似度算法
similarity Elasticsearch allows you to configure a scoring algorithm or similarity per field. The si ...
- sql 查询所有数据库-表-表结构
--查询数据库中的所有数据库名: SELECT * FROM Master..SysDatabases ORDER BY Name --查询某个数据库中所有的表名: select * from sys ...
- MySQL内置函数uuid和uuid_short
MySQL的uuid这个函数.简要介绍一下. 用法 简单看到,这个值,每次执行都是不同的. 生成规则 第1 2 3 段是与时间有关的. time_low.time_mid.time_high_and_ ...
- vmware workstation pro 14 虚拟机无法开启、黑屏的解决方案汇总
方案1:卸载鲁大师,重启. 方案2:管理员命令行,输入netsh winsock reset,重启. 方案3:360安全管家修复LSP,重启. 方案4:卸载14.0,安装12.0,手动导入虚拟机.
- Ubuntu 16.04下安装64位谷歌Chromium(Chrome)浏览器
在命令行下输入: sudo add-apt-repository ppa:a-v-shkop/chromium sudo apt-get update sudo apt-get install chr ...
- Mysql表引擎的切换
转换MYSQL表的引擎 1.方法一:Alter table 将1个表的引擎修改为另一个引擎最简单的方法是使用Alter table语句. 下面的语句将myTable的引擎修改为InnoDB mysql ...
- ui5 load josn
sap.ui.jsview("ui5p.Test01", { /** Specifies the Controller belonging to this View. * In t ...
- C#网络编程—HTTP应用编程(转)
https://www.cnblogs.com/huangxincheng/archive/2012/01/09/2316745.html https://www.cnblogs.com/wangqi ...