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的更多相关文章

  1. 基于eclipse-java的平台上搭建安卓开发环境

    首先感谢好人的分享!http://www.mamicode.com/info-detail-516839.html 系统:windows 7 若想直接安装eclipse—android的,请启动如下传 ...

  2. 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题

    刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...

  3. Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境

    我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...

  4. 安卓开发第一步:Android Studio安装配置

    虽然本人是JAVA开发工程师平时主要开发Web App,但因为项目需求需要开发对应的移动端.一时又找不到合适的安卓开发人员,兄弟我只好被项目经理"抓来当壮丁了".俗话说好" ...

  5. monkeyrunner之安卓开发环境搭建(一)

    在学习monkeyrunner之前,让我们先搭建好eclipse安卓开发环境. 对于程序开发人员而言,eclipse并不陌生,它提供了一个非常广阔的平台来开发程序.同样也可以用它来开发android程 ...

  6. 安卓开发:效果图中标注的像素尺寸如何转换为安卓的dp尺寸?

    我们的UI基于1920x1080分辨率给的尺寸标注,但是在安卓开发中大家一般都使用dp.sp来标注界面尺寸,所以需要一个dp与sp的转换公式. 一开始参考的的这篇文章:关于Android开发中px.d ...

  7. 安卓开发30:AsyncTask的用法

    http://blog.csdn.net/initphp/article/details/10392093 安卓开发笔记系列(43)  在开发Android应用时必须遵守单线程模型的原则: Andro ...

  8. delphi XE5下安卓开发技巧

    delphi XE5下安卓开发技巧 一.手机快捷方式显示中文名称 project->options->Version Info-label(改成需要显示的中文名即可),但是需要安装到安卓手 ...

  9. 推荐 greenrobot eventbus,简化安卓开发,提高安卓维护性,优化安卓性能

    最近在研究移动开发,广泛的阅读有关cordova,apicloud,android资料.发现安卓的开发还是很简单的.再发现greenrobot eventbus开源框架不仅可以简化安卓开发,有可以大幅 ...

随机推荐

  1. 史上最全: svn与git的对照(二):svn与git的相关概念

    如图1是svnserver端数据的文件夹结构 以下是gitserver端的文件夹结构 纵观svn和git服务端的文件夹结构我们非常easy发现 1.有些目录还是蛮像的.甚至是一样的比方说svn中的co ...

  2. [Struts2] No result defined for action ... and result input &amp; Invalid field value for field ...

    "No result defined for action ... and result input"错误一般发生在Struts2的拦截器拦截时遇到了问题时.Struts2会将跳转 ...

  3. Android程序全然退出的三种方法

    1. Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid())    //获取PID,眼下获取自己的也仅仅有该 ...

  4. 2016 ICPC CAMP Recording

    等了好久终于等到今天 马上能和群巨们一起学习了 希望不要暴露我太弱的本质............ 北京不冷,就是风大~~~ 1.24 8点准时起床了,准备下楼吃早饭 (这个宾馆好多美美的空姐对面就是东 ...

  5. ServiceStack.Redis之IRedisClient<第三篇>【转】

    事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...

  6. jQuery学习(一)——jQuery入门

    1.jQuery基础 Jquery它是一个库(框架),要想使用它,必须先引入! jquery-1.8.3.js:一般用于学习阶段. jquery-1.8.3.min.js:用于项目使用阶段 官网下载后 ...

  7. c#0218-命名空间

    1 namespace 命名空间 可以解决类的重命名问题 可以看做是类的文件夹: 2 跨项目使用类 一个解决方案下有不同的项目,如果想在一个项目中引用另一个项目的类,解决方法是 1 添加引用 2 引用 ...

  8. POJ 2481 Cows【树状数组】

    题意:给出n头牛的s,e 如果有两头牛,现在si <= sj && ei >= ej 那么称牛i比牛j强壮 然后问每头牛都有几头牛比它强壮 先按照s从小到大排序,然后用e来 ...

  9. 创建私有CA, 加密解密基础, PKI, SSL

    发现了一篇图文并茂的超棒的  加密解密, CA, PKI, SSL 的基础文章, 链接如下:https://blog.csdn.net/lifetragedy/article/details/5223 ...

  10. HDU-4370 '0 or 1' 最短路 要考虑连通性

    题目链接:https://cn.vjudge.net/problem/HDU-4370 题意 给一个矩阵C(nn),要我们找到一个矩阵X(nn),满足以下条件: X_{12}+X_{13}+...X_ ...