欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android post请求接口demo

程序员文章站 2023-12-20 22:30:28
本文实例为大家分享了android post请求接口demo测试代码,供大家参考,具体内容如下 mainactivity.java package com.ts...

本文实例为大家分享了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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: