首页 > 代码库 > 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