android post请求接口demo
本文实例为大家分享了androidpost请求接口demo测试代码,供大家参考,具体内容如下
MainActivity.java
packagecom.tsh.test; importjava.io.InputStream; importjava.io.OutputStream; importjava.io.PrintWriter; importjava.net.HttpURLConnection; importjava.net.URL; importandroid.app.Activity; importandroid.content.Intent; importandroid.os.Bundle; importandroid.os.Handler; importandroid.os.Message; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.TextView; importandroid.widget.Toast; publicclassMainActivityextendsActivity{ publicButtonloginBtn; publicTextViewloginUserName; publicTextViewloginPassword; publicstaticStringAPI="http://mail.sina.net/loginxxx"; publicLoginHandlerloginHandler; @Override protectedvoidonCreate(BundlesavedInstanceState){ 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(newOnClickListener(){ @Override publicvoidonClick(Viewarg0){ //开启新线程 ThreadloginThread=newThread(newLoginRunable()); loginThread.start(); } }); loginHandler=newLoginHandler(); } //实现Runable接口,开启新线程 classLoginRunableimplementsRunnable{ @Override publicvoidrun(){ try{ URLurl=newURL(API); HttpURLConnectionhttp=(HttpURLConnection)url.openConnection(); http.setRequestMethod("POST"); http.setDoInput(true); http.setDoOutput(true); OutputStreamops=http.getOutputStream(); PrintWriterpw=newPrintWriter(ops); Stringusername=loginUserName.getText().toString(); Stringpassword=loginPassword.getText().toString(); pw.write("email="+username+"&psw="+password+"&loginfrom=app&output=json"); pw.flush(); InputStreamins=http.getInputStream(); byte[]buffer=newbyte[1024]; intlength=0; StringBuildersb=newStringBuilder(); while((length=ins.read(buffer))!=-1){ sb.append(newString(buffer,0,length)); } Messagemsg=newMessage(); msg.what=1; msg.obj=sb.toString(); loginHandler.sendMessage(msg); }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } //传递消息的handle classLoginHandlerextendsHandler{ @Override publicvoidhandleMessage(Messagemsg){ StringloginResponse=(String)msg.obj; System.out.println(loginResponse); Toast.makeText(MainActivity.this,loginResponse,10).show(); Intentintent=newIntent(MainActivity.this,MailIndexActivity.class); //startActivity(intent); } } }
main_activity.xml
<LinearLayoutxmlns: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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。