Android——JDK的get请求方式
Posted 柒寒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android——JDK的get请求方式相关的知识,希望对你有一定的参考价值。
layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp3.TestActivity2" 11 android:orientation="vertical"> 12 13 <Button 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:text="JDK-Get方式" 17 android:onClick="bt1_onClick"/> 18 <EditText 19 android:layout_width="match_parent" 20 android:layout_height="200dp" 21 android:id="@+id/et_2"/> 22 </LinearLayout>
java类:
1 package com.hanqi.testapp3; 2 3 import android.app.ProgressDialog; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 10 import java.io.InputStream; 11 import java.net.HttpURLConnection; 12 import java.net.URL; 13 14 public class TestActivity2 extends AppCompatActivity { 15 16 EditText et_2; 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_test2); 21 et_2 = (EditText)findViewById(R.id.et_2); 22 } 23 //JDK的Get方式 24 public void bt1_onClick(View v) 25 { 26 //1.进度对话框 27 final ProgressDialog progressDialog = ProgressDialog.show(this,null,"正在加载,请稍后……"); 28 //2.开启子线程,访问网络 29 new Thread(){ 30 public void run() 31 { 32 try { 33 //1—URL 34 URL url = new URL("http://www.baidu.com"+"?name=tom"); 35 36 //2—URL获取连接 37 HttpURLConnection huc = (HttpURLConnection)url.openConnection(); 38 //请求方式 39 huc.setRequestMethod("GET"); 40 //设置超时 41 huc.setConnectTimeout(3000); 42 huc.setReadTimeout(3000); 43 //连接并发送请求 44 huc.connect(); 45 //接收 46 //判断返回状态码 200 47 int code = huc.getResponseCode(); 48 if (code == 200) 49 { 50 //接收数据 51 //输入流 52 InputStream is = huc.getInputStream(); 53 //读取流 54 //1—byte数组 55 byte[] b = new byte[1024]; 56 //2—读到数组的长度 57 int i = 0; 58 //3—数据 59 final StringBuilder sb1 = new StringBuilder(); 60 while ((i = is.read(b))>0) 61 { 62 sb1.append(new String(b,0,i)); 63 } 64 //释放资源 65 is.close(); 66 huc.disconnect(); 67 //通过主线程显示信息和关闭对话框 68 runOnUiThread(new Runnable() { 69 @Override 70 public void run() { 71 et_2.setText(sb1); 72 progressDialog.dismiss(); 73 } 74 }); 75 } 76 else 77 { 78 Toast.makeText(TestActivity2.this, "连接错误,返回的状态码 = "+code, Toast.LENGTH_SHORT).show(); 79 } 80 } 81 catch (Exception e) 82 { 83 e.printStackTrace(); 84 progressDialog.dismiss(); 85 } 86 } 87 }.start(); 88 } 89 }
以上是关于Android——JDK的get请求方式的主要内容,如果未能解决你的问题,请参考以下文章
Android传统HTTP请求get----post方式提交数据(包括乱码问题)