安卓开发--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开源框架不仅可以简化安卓开发,有可以大幅 ...
随机推荐
- C++开发人脸性别识别教程(7)——搭建MFC框架之界面绘制
在之前的博客中我们已经将项目中用到的算法表述完成,包含人脸检測算法以及四种性别识别算法,在这篇博客中我们将着手搭建主要的MFC框架. 一.框架概况 在这篇博文中我们将搭建最主要的MFC框架.绘制MFC ...
- Request的getParameter和getAttribute方法的差别
HttpServletRequest.getParameter("modelName");能取到想要的modelObject吗?经过測试之后.发现是不能的. 后来想想.其它道理挺简 ...
- QT中|Qt::Tool类型窗口自动退出消息循环问题解决(setQuitOnLastWindowClosed必须设置为false,最后一个窗口不显示的时候,程序会退出消息循环)
为application 设置setQuitOnLastWindowClosed属性,确实为true: 将其显示为false; 退出该应该程序不能调用QDialog的close消息槽,只能调用qApp ...
- spark学习及环境配置
http://dblab.xmu.edu.cn/blog/spark/ 厦大数据库实验室博客 总结.分享.收获 实验室主页 首页 大数据 数据库 数据挖掘 其他 子雨大数据之Spark入门教程 林子 ...
- bzoj1797: [Ahoi2009]Mincut 最小割(最小割+强联通tarjan)
1797: [Ahoi2009]Mincut 最小割 题目:传送门 题解: 感觉是一道肥肠好的题目. 第二问其实比第一问简单? 用残余网络跑强联通,流量大于0才访问. 那么如果两个点所属的联通分量分别 ...
- doT.js的使用
引言 doT.js可以更好的在html端使用json数据. {{ }} for evaluation 模板标记符 {{= }} for interpolation 输出显示,默认变量名叫it {{! ...
- java类型与Hadoop类型之间的转换
java基本类型与Hadoop常见基本类型的对照Long LongWritableInteger IntWritableBoolean BooleanWritable String Text ...
- Android TextView加下划线的几种方式
如果是在资源文件里: <resources> <</u></string> <string name="app_name">M ...
- SQLServer 错误: 15404,维护计划无法执行
错误症状: D:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG下面的ERROELOG,用文本打,查看运行维维计划不成功是生成的错误日志详细信 ...
- PKI和加密,散列算法
Day 11-PKI和加密,散列算法 PKI(Public Key Infrastructure公钥基础设施) 1 PKI(Public Key Infrastructure公钥基础设施)回顾学习 - ...