[android] post请求接口demo测试代码
MainActivity.java
package com.tsh.test; import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity {
public Button loginBtn;
public TextView loginUserName;
public TextView loginPassword;
public static String API="http://mail.sina.net/loginxxx";
public LoginHandler loginHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取View对象
loginBtn=(Button) findViewById(R.id.loginBtn);
loginUserName=(TextView) findViewById(R.id.loginUsername);
loginPassword=(TextView) findViewById(R.id.loginPassword);
//给View对象设置点击事件
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//开启新线程
Thread loginThread=new Thread(new LoginRunable());
loginThread.start();
}
});
loginHandler=new LoginHandler();
}
//实现Runable接口,开启新线程
class LoginRunable implements Runnable{
@Override
public void run() {
try {
URL url=new URL(API);
HttpURLConnection http=(HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setDoInput(true);
http.setDoOutput(true);
OutputStream ops=http.getOutputStream();
PrintWriter pw=new PrintWriter(ops);
String username=loginUserName.getText().toString();
String password=loginPassword.getText().toString();
pw.write("email="+username+"&psw="+password+"&loginfrom=app&output=json");
pw.flush(); InputStream ins=http.getInputStream();
byte[] buffer = new byte[1024];
int length=0;
StringBuilder sb=new StringBuilder();
while((length=ins.read(buffer))!=-1){
sb.append(new String(buffer,0,length));
} Message msg=new Message();
msg.what=1;
msg.obj=sb.toString();
loginHandler.sendMessage(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
//传递消息的handle
class LoginHandler extends Handler{
@Override
public void handleMessage(Message msg) {
String loginResponse=(String) msg.obj;
System.out.println(loginResponse);
Toast.makeText(MainActivity.this, loginResponse, 10).show();
Intent intent=new Intent(MainActivity.this, MailIndexActivity.class);
//startActivity(intent);
}
}
}
main_activity.xml
<LinearLayout 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:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />
<EditText
android:hint="请输入用户名"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginUsername"
android:text="shihan@appdev.sinanet.com"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"/>
<EditText
android:hint="请输入密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginPassword"
android:text="xxxxxxx"/>
<Button
android:id="@+id/loginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆认证"
/>
</LinearLayout>
[android] post请求接口demo测试代码的更多相关文章
- Android : 获取声卡信息的测试代码
完整的编译包(android平台): 链接:http://pan.baidu.com/s/1qXMTT7I 密码:2bow /* * ALSA parameter test program * * C ...
- Android 网络请求框架Retrofit
Retrofit是Square公司开发的一款针对Android网络请求的框架,Retrofit2底层基于OkHttp实现的,OkHttp现在已经得到Google官方认可,大量的app都采用OkHttp ...
- Go项目的测试代码2(项目运用)
上一篇文章介绍了最基本的测试代码的写法.Go项目的测试代码(基础) 这里简单的共享一下我在项目中使用的方式. 项目结构 我们实际项目中, 结构简单地分了控制层controllers和模块层models ...
- RobotFrameWork接口报文测试-----(三)demo的加强版(数据驱动测试)
在上一篇RobotFrameWork接口报文测试-----(二)demo的升级版基础上,将接口的xml的格式保存在xml文件中,然后程序如果增加一个接口,在xml文件里添加即可,无需修改自动化测试里的 ...
- Android网络请求框架AsyncHttpClient实例详解(配合JSON解析调用接口)
最近做项目要求使用到网络,想来想去选择了AsyncHttpClient框架开进行APP开发.在这里把我工作期间遇到的问题以及对AsyncHttpClient的使用经验做出相应总结,希望能对您的学习有所 ...
- Axis2创建WebService服务端接口+SoupUI以及Client端demo测试调用
第一步:引入axis2相关jar包,如果是pom项目,直接在pom文件中引入依赖就好 <dependency> <groupId>org.apache.axis2</gr ...
- Java 银联支付官网demo测试及项目整合代码
注:原文来源与 < Java 银联支付官网demo测试及项目整合代码 > 银联支付(网关支付B2C) 一.测试官网demo a)下载官网开发包,导入eclipse等待修改(下载的开发包没 ...
- 【Golang 接口自动化08】使用标准库httptest完成HTTP请求的Mock测试
前言 Mock是一个做自动化测试永远绕不过去的话题.本文主要介绍使用标准库net/http/httptest完成HTTP请求的Mock的测试方法. 可能有的小伙伴不太了解mock在实际自动化测试过程中 ...
- 自动化接口差异测试-diffy 回归测试 charles rewrite 请求
https://mp.weixin.qq.com/s/vIxbtQtRRqgYCrDy7XTcrA 自动化接口差异测试-diffy Boris 搜狗测试 2018-08-30 自动化接口差异测试- ...
随机推荐
- 526. Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- for循环 | range 对象
# ### for循环 # 循环 遍历 迭代 # 把列表的元素一一的拿出来遍历 listvar = ["黄雄大","黄文","黄仪正",&q ...
- bzoj2662冻结
题目链接 话说为什么出题人老是卡$SPFA$啊$qwq$ 然而$SPFA$硬是让本宝宝写成了$dij$ 分情况讨论就好 /*************************************** ...
- Java面向对象之异常(throw与throws)
一.基础概念 1.throw和throws的区别: 位置不同:throws用在函数上,后面跟的是异常类,可以跟多个. throw用在函数内,后面跟的是异常对象. 功能不同:throws用来声明异常,让 ...
- ArchLinux中证书错误解决方案
ca-certificates 更新 x509: failed to load system roots and no roots provided. curl error: Problem with ...
- docker部署sftp
一. 按照我博客中搭建sftp的方法做一个docker镜像 这种方法可用,但不是最好的,待改进.可参照另一篇博客:设置多用户不同权限的sftp服务器搭建 1. dockerfile文件如下,当前目录假 ...
- mysql 存储过程和游标
CREATE DEFINER=`root`@`localhost` PROCEDURE `NewProc`() BEGIN #Routine body goes here... DECLARE ite ...
- node.js调试方法
第一种方式:node内置的调试器 在程序中添加debugger,然后在启动node程序时,使用debug模式启动 1.node debug my_event.js 2.使用node文档中各种命令,进行 ...
- Android Fragment实现微信底部导航
1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...
- 进阶篇:5.3.1)均方根法(Root-Sum-Squares,RSS)
本章目的:了解均方根法,运用均方根法. 1.定义 均方根法(Root-Sum-Squares,RSS):均方根法是统计分析法的一种,是把尺寸链中的各个尺寸公差的平方之和再开根即得到关键尺寸的公差. 其 ...