查看: 1376|回复: 0

[手机开发] android post请求接口demo

发表于 2017-8-6 08:53:33
尚学堂AD

本文实例为大家分享了android post请求接口demo测试代码,供大家参考,具体内容如下

MainActivity.java

  1. package com.tsh.test;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.os.Handler;
  11. import android.os.Message;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17. public class MainActivity extends Activity {
  18. public Button loginBtn;
  19. public TextView loginUserName;
  20. public TextView loginPassword;
  21. public static String API="http://mail.sina.net/loginxxx";
  22. public LoginHandler loginHandler;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main);
  27. //获取View对象
  28. loginBtn=(Button) findViewById(R.id.loginBtn);
  29. loginUserName=(TextView) findViewById(R.id.loginUsername);
  30. loginPassword=(TextView) findViewById(R.id.loginPassword);
  31. //给View对象设置点击事件
  32. loginBtn.setOnClickListener(new OnClickListener() {
  33. @Override
  34. public void onClick(View arg0) {
  35. //开启新线程
  36. Thread loginThread=new Thread(new LoginRunable());
  37. loginThread.start();
  38. }
  39. });
  40. loginHandler=new LoginHandler();
  41. }
  42. //实现Runable接口,开启新线程
  43. class LoginRunable implements Runnable{
  44. @Override
  45. public void run() {
  46. try {
  47. URL url=new URL(API);
  48. HttpURLConnection http=(HttpURLConnection) url.openConnection();
  49. http.setRequestMethod("POST");
  50. http.setDoInput(true);
  51. http.setDoOutput(true);
  52. OutputStream ops=http.getOutputStream();
  53. PrintWriter pw=new PrintWriter(ops);
  54. String username=loginUserName.getText().toString();
  55. String password=loginPassword.getText().toString();
  56. pw.write("email="+username+"&psw="+password+"&loginfrom=app&output=json");
  57. pw.flush();
  58. InputStream ins=http.getInputStream();
  59. byte[] buffer = new byte[1024];
  60. int length=0;
  61. StringBuilder sb=new StringBuilder();
  62. while((length=ins.read(buffer))!=-1){
  63. sb.append(new String(buffer,0,length));
  64. }
  65. Message msg=new Message();
  66. msg.what=1;
  67. msg.obj=sb.toString();
  68. loginHandler.sendMessage(msg);
  69. } catch (Exception e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. //传递消息的handle
  76. class LoginHandler extends Handler{
  77. @Override
  78. public void handleMessage(Message msg) {
  79. String loginResponse=(String) msg.obj;
  80. System.out.println(loginResponse);
  81. Toast.makeText(MainActivity.this, loginResponse, 10).show();
  82. Intent intent=new Intent(MainActivity.this, MailIndexActivity.class);
  83. //startActivity(intent);
  84. }
  85. }
  86. }
复制代码

main_activity.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context="${relativePackage}.${activityClass}" >
  7. <TextView
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="用户名" />
  11. <EditText
  12. android:hint="请输入用户名"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:id="@+id/loginUsername"
  16. android:text="shihan@appdev.sinanet.com"
  17. />
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="密码"/>
  22. <EditText
  23. android:hint="请输入密码"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:id="@+id/loginPassword"
  27. android:text="xxxxxxx"/>
  28. <Button
  29. android:id="@+id/loginBtn"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="登陆认证"
  33. />
  34. </LinearLayout>
复制代码

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



回复

使用道具 举报