标签:android blog class c code java
<!-- 添加网络访问权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
布局文件 activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:layout_alignBaseline="@+id/editusername" android:text="@string/username"/> <EditText android:id="@+id/editusername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/username" android:hint="@string/edit_username" /> <TextView android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:layout_below="@+id/username" android:layout_alignBaseline="@+id/editpass" android:text="@string/password"/> <EditText android:id="@+id/editpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/password" android:layout_below="@+id/editusername" android:hint="@string/edit_password" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:layout_below="@+id/password" android:layout_marginLeft="50dp" android:text="@string/btn_sure" android:onClick="login" /> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_alignBaseline="@+id/btn_login" android:layout_below="@+id/editpass" android:layout_toRightOf="@+id/btn_login" android:text="@string/checkbox" /> <TextView android:id="@+id/tv_result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/tip_result"/> </RelativeLayout>
String.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">lesson03</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="username">用户名</string> <string name="password">密 码</string> <string name="edit_username">请输入用户名</string> <string name="edit_password">请输入密码</string> <string name="btn_sure">确定</string> <string name="checkbox">是否记住密码</string> <string name="tip_result">服务器返回的数据</string> </resources>
流转字符工具类 StreamTools.java
package com.example.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamTools { /** * 把流轉換成字符串 * @param is * @return */ public static String streamToStr(InputStream is){ try { // 字节的输出流 ByteArrayOutputStream os = new ByteArrayOutputStream(); // 定义读取长度 int len = 0; // 定义缓冲区 byte buffer[] = new byte[1024]; // 从输入流中读取,并写入os对象中 while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } // 关闭流 is.close(); os.close(); // 写到字节流 return new String(os.toByteArray()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
Activity类 LoginActivity.java
package com.example.android_http; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.example.util.StreamTools; public class LoginActivity extends Activity { private EditText et_username; private EditText et_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); findView(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.login, menu); return true; } public void findView() { et_password = (EditText) findViewById(R.id.editpass); et_username = (EditText) findViewById(R.id.editusername); } public void login(View v) { // 获取点击控件的id int id = v.getId(); // 根据id进行判断 switch (id) { case R.id.btn_login:// 进行登录操作 // Toast.makeText(getApplicationContext(), "--", 0).show(); // 获取用户名密码 final String userName = et_username.getText().toString(); final String userPass = et_password.getText().toString(); // 判断是否为空 if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPass)) { Toast.makeText(getApplicationContext(), "用户名或者密码不能为空", 0) .show(); } else { Toast.makeText(getApplicationContext(), "發送請求道服務器", 0) .show(); // 访问网络(需要一个网络的权限) // 访问网络(好事操作)子线程,避免阻塞主线程UI // http://172.16.237.200:8080/video/login.do?username=chj&userpass=123 // 所有耗時操作都要在子線程中 new Thread() { public void run() { try { // 请求地址 String spec = "http://172.16.237.200:8080/video/login.do?username=" + userName + "&userpass=" + userPass; // 根据地址创建URL对象(网络访问url) URL url = new URL(spec); // 采用http协议打开的连接对象 HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); urlConnection.setRequestMethod("GET");// 以get方式发起请求 urlConnection.setReadTimeout(5000);// 设置超时 urlConnection.setConnectTimeout(5000);// 设置连接超时 urlConnection .setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"); // 获取相应的 code 400 200 505 302 if (urlConnection.getResponseCode() == 200) { // 得到网络返回的输入流 InputStream is = urlConnection.getInputStream(); String result = StreamTools.streamToStr(is); System.out.println("返回的数据是:" + result); } else { System.out.println("请求url失败"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } break; default: break; } } }
赵雅智_android http get请求,布布扣,bubuko.com
标签:android blog class c code java
原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/26509763