get请求
来源:互联网 发布:双十一数据增长文献 编辑:程序博客网 时间:2024/06/04 00:25
package com.example.unit3_requestdata_asynctask_demo;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.view.Menu; import android.view.View; import android.widget.TextView;
public class MainActivity extends Activity {
private ProgressDialog pd;private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv);}public void requestjoke(View v){ //1.构造出来 MyTask myTask=new MyTask(); myTask.execute("http://japi.juhe.cn/joke/content/list.from?sort=asc&page=1&pagesize=10&time=1418816972&key=fae8fe09dba9d27aab24308a21b53bce");}//定义异步任务//1.Params 一般是请求的网址 String Void //2.Progress 请求网络数据的进度 一般是 Integer void //3.Result 请求网络数据得到的结果 json xml String Void 自定义一个对象 List<?> class MyTask extends AsyncTask<String, Void, String>{ //请求网络 的代码 @Override protected String doInBackground(String... arg0) {//String... 可变长类型 相当于String[] String str=""; try { //1.根据路径创建一个URL对象 URL url=new URL(arg0[0]); //2.打开连接 HttpURLConnection openConnection = (HttpURLConnection)url.openConnection(); //3.进行设置 openConnection.setRequestMethod("GET"); openConnection.setReadTimeout(3000); openConnection.setConnectTimeout(3000); //4.得到响应码 int code=openConnection.getResponseCode(); if(code==200){ //5.得到数据 InputStream inputStream = openConnection.getInputStream(); str=streamToString(inputStream); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } //在执行操作之前 @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); //创建一个进度对话框,并显示出来 pd=new ProgressDialog(MainActivity.this); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);//小圆 设置样式 pd.setMessage("正在请求,请稍后..."); pd.show(); } //在执行操作之后 @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); if(pd!=null && pd.isShowing()){ //关闭对话框 pd.dismiss(); } //显示 tv.setText(result); }}/** * 将字节流信息的内容读取出来 * @param is * @return */public String streamToString(InputStream is){ StringBuilder builder=new StringBuilder(); BufferedReader reader=new BufferedReader(new InputStreamReader(is)); String con; try { while((con=reader.readLine())!=null){ builder.append(con); } reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return builder.toString();}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" ><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请求笑话数据" android:onClick="requestjoke" android:id="@+id/btn"/><TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv" android:layout_below="@+id/btn" android:textColor="#00f"/></RelativeLayout>
阅读全文
0 0
- get请求
- Get请求
- get请求
- GET请求
- get请求
- GET请求
- get请求
- get请求
- GET请求 Post请求
- get请求 post请求
- Get请求 Post请求
- get请求和post请求
- get请求和post请求
- GET请求和POST请求
- IOS-post请求、get请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- 数据结构实验之链表二:逆序建立链表
- 清北学堂-D7-T1-bst
- Computer Architecture Background
- centos上安装samtool具体步骤
- Html5_animation动画
- get请求
- leetcode 105 /106 . Construct Binary Tree
- Session实现原理深入讨论
- 15链表中倒数第K个节点python
- bzoj1610 [Usaco2008 Feb]Line连线游戏
- 堆及相关问题
- 第四周项目四 猴子选大王
- Unity3d打包APK出现的问题—无法找到tool.jar
- 21. Merge Two Sorted Lists