[android] post请求接口demo测试代码

[android] post请求接口demo测试代码,第1张

概述MainActivity.java main_activity.xml

MainActivity.java

package com.tsh.test;import java.io.inputStream; java.io.OutputStream; java.io.PrintWriter; java.net.httpURLConnection; java.net.URL; androID.app.Activity; androID.content.Intent; androID.os.Bundle; androID.os.Handler; androID.os.Message; androID.vIEw.VIEw; androID.vIEw.VIEw.OnClickListener; androID.Widget.button; androID.Widget.TextVIEw; androID.Widget.Toast;public class MainActivity extends Activity {    public button loginBtn;     TextVIEw loginUsername;     TextVIEw loginPassword;    static String API="http://mail.sina.net/loginxxx";     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             onClick(VIEw arg0) {                开启新线程                Thread loginThread=new Thread( LoginRunable());                loginThread.start();            }        });        loginHandler= LoginHandler();    }    实现Runable接口,开启新线程    class LoginRunable implements Runnable{        @OverrIDe         run() {            try {                URL url= URL(API);                httpURLConnection http=(httpURLConnection) url.openConnection();                http.setRequestMethod("POST");                http.setDoinput(true);                http.setDoOutput();                OutputStream ops=http.getoutputStream();                PrintWriter pw= 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= StringBuilder();                while((length=ins.read(buffer))!=-1){                    sb.append(new String(buffer,0,length));                }                                Message msg= Message();                msg.what=1;                msg.obj=sb.toString();                loginHandler.sendMessage(msg);            } catch (Exception e) {                 Todo auto-generated catch block                e.printstacktrace();            }                    }    }    传递消息的handle    class LoginHandler  Handler{        @OverrIDe         handleMessage(Message msg) {            String loginResponse=(String) msg.obj;            System.out.println(loginResponse);            Toast.makeText(MainActivity.this,loginResponse,10).show();            Intent intent=new Intent(MainActivity.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:ID="@+ID/loginUsername"        androID:text="shihan@appdev.sinanet.com"        />    <        androID:text="密码"/>    <EditText         androID:hint="请输入密码"        androID:ID="@+ID/loginPassword"        androID:text="xxxxxxx"/>    <button         androID:ID="@+ID/loginBtn"        androID:text="登陆认证"        /></linearLayout>

 

总结

以上是内存溢出为你收集整理的[android] post请求接口demo测试代码全部内容,希望文章能够帮你解决[android] post请求接口demo测试代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1121192.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存