从服务器请求数据

时间:2021-11-24 17:56:57

1.实例化一个url对象
2.获取HttpUrlConnection对象
3.设置请求连接属性
4.获取响应码,判断是否连接成功
5.读取输入流并解析


从服务器请求数据


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.studio.imoocdemo.MainActivity">


<TextView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#3f51b5"
android:gravity="center"
android:paddingRight="15dp"
android:text="慕课"
android:textColor="#ffffff"
android:textSize="28sp" />


<ImageView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY" />


<ListView
android:id="@+id/essay_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>

</LinearLayout>

MainActivity.java

package com.studio.imoocdemo;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initview();
}

public void initview() {
findViewById(R.id.header).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, DetailActivity.class));
}
});
}
}

activity_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.studio.imoocdemo.DetailActivity">



<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:textSize="24sp" />


<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="right"
android:paddingRight="10dp"
android:textSize="20sp" />


<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:lineSpacingMultiplier="1.5"
android:textSize="20sp" />


</LinearLayout>

DetailActivity.java

package com.studio.imoocdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DetailActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

initData();
}

public void initData() {
//HttpUrlConnection
/**
* 1.实例化一个url对象
* 2.获取HttpUrlConnection对象
* 3.设置请求连接属性
* 4.获取响应码,判断是否连接成功
* 5.读取输入流并解析
*/

//参数:你要访问的接口地址
new Thread() {
@Override
public void run() {
super.run();
try {
URL url = new URL("http://www.imooc.com/api/teacher?type=3&cid=1");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(6000);
//获取响应码
//200表示服务器成功返回网页;404(未找到)服务器找不到请求的网页;
//500(服务器内部错误)服务器遇到错误,无法完成请求;503服务不可用。
if (httpURLConnection.getResponseCode() == 200) {
//获取输入流
InputStream inputStream = httpURLConnection.getInputStream();
byte[] bytes = new byte[1024 * 512];
int len = 0;
//建立缓存流,保存所读取的字节数组
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(bytes)) > -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
String msg = byteArrayOutputStream.toString();
Log.e("TAG", msg);

}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();

}
}

从服务器请求数据