首页 > 代码库 > wenti
wenti
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.administrator.money104.MainActivity">
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:id="@+id/lv_main">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="http://www.mamicode.com/@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="4dp"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:maxLines="1"
android:text="Title"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:maxLines="3"
android:text="content"
android:id="@+id/textView" />
</LinearLayout>
</LinearLayout>
package com.example.administrator.money104;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public ListView mListView;
public static final String URL = "http://www.imooc.com/api/teacher?type=4&num=30";
String jsonString = null;
String line ="";
BufferedReader br;
InputStreamReader isr;
String result = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.lv_main);
new NewAsyncTask().execute(URL);
}
class NewAsyncTask extends AsyncTask<String,Void,List<NewBean>>{
@Override
protected List<NewBean> doInBackground(String... params) {
return getJsonDate(params[0]);//请求网址
}
}
private List<NewBean> getJsonDate(String url) {
List<NewBean> newListNewBean = new ArrayList<>();
try {
jsonString = readStream(new URL(url).openStream());//URL 为 net的URL**********
} catch (IOException e) {
e.printStackTrace();
}
Log.d("lld",jsonString);
return newListNewBean;
}
private String readStream(InputStream is)
{
try {
isr = new InputStreamReader(is,"utf-8");//字节流转化为字符流
br = new BufferedReader(isr);
while ((line = br.readLine())!= null){
result += result;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
wenti
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。