适配器:Myadapter
package com.bawi.adapter;
}).start();
}
Bean类
package com.bawi.Bean;
public class Bean {
public String url;
public String title;
public String date;
public String category;
public String author_name;
public String thumbnail_pic_s;
}
主界面:MainActivity
package com.bawi.httpclient_lx;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.bawi.Bean.Bean;
import com.bawi.adapter.Myadapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ImageView tv_image;
private String responsemsg;
private Myadapter ma;
private ListView lv;
private ArrayList
private final int SUCCESS=0;
private final int FALT=-1;
private final String GETURL="http://v.juhe.cn/toutiao/index?type=top&&key=22a108244dbb8d1f49967cd74a0c144d";
Handler myhandler=new Handler() {
public void handleMessage(Message msg){
int what = msg.what;
switch (what){
case SUCCESS:
String result = (String) msg.obj;
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
pareseDATA(result);
break;
case FALT:
Toast.makeText(MainActivity.this,responsemsg,Toast.LENGTH_SHORT).show();
break;
}
}
};
//JSON解析
private void pareseDATA(String result) {
try {
JSONObject obj=new JSONObject(result);
JSONObject resultobj = obj.getJSONObject("result");
JSONArray dataArray = resultobj.getJSONArray("data");
if(dataArray.length()>0&&dataArray!=null)
{
for (int i = 0; i
Bean bean=new Bean();
bean.title= data.getString("title");
bean.date=data.getString("date");
bean.thumbnail_pic_s=data.getString("thumbnail_pic_s");
bean.category=data.getString("category");
bean.url=data.optString("url");
list.add(bean);
}
System.out.println(list);
flushnum();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
//创建线程
new Thread(new Runnable() {
@Override
public void run() {
getNews();
}
}).start();
}
private void getNews() {
StringBuffer result=new StringBuffer();
//创建HttpClient对象,打开一个浏览器
HttpClient httpclient = new DefaultHttpClient();
//创建HttpGet对象,get请求对象,传入url
HttpGet httpget=new HttpGet(GETURL);
try {
//发送请求,返回Httprespones对象,这是服务器返回的对象
HttpResponse httpResponse = httpclient.execute(httpget);
//根据相应行,拿到响应码,getStatusLine:相应行;; getStatusCode:响应码
int responseCode = httpResponse.getStatusLine().getStatusCode();
responsemsg = httpResponse.getStatusLine().getReasonPhrase();
//判断响应码是否为200,如果是则请求成功否则失败
if(200==responseCode)
{
//得到返回的实体对象
HttpEntity entity = httpResponse.getEntity();
InputStream inputStream = entity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String line=null;
while((line=reader.readLine())!=null)
{
result.append(line);
}
Message msg=new Message();
msg.what=SUCCESS;
msg.obj=result.toString();
myhandler.sendMessage(msg);
}else
{
Message msg=new Message();
msg.what=FALT;
myhandler.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
httpclient.getConnectionManager().shutdown();
}
}
private void initData() {
list=new ArrayList
ma=new Myadapter(this,list);
}
private void flushnum()
{
ma=new Myadapter(this,list);
lv.setAdapter(ma);
lv.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Bean bean = (Bean) ma.getItem(i);
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
intent.putExtra("url",bean.url);
intent.putExtra("title",bean.title);
startActivity(intent);
}
});
}
//初始化数据
private void initView() {
lv= (ListView) findViewById(R.id.lv);
}
}
webView获取
package com.bawi.httpclient_lx;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebView;
public class OtherActivity extends AppCompatActivity {
private WebView wv;
private String title;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
initView();
initData();
LoadWebView();
}
private void initData() {
if(getIntent().getExtras()!=null)
{
title = getIntent().getExtras().getString("title");
url=getIntent().getExtras().getString("url");
if(!TextUtils.isEmpty(title))
{
setTitle(title);
}else
{
setTitle("详情");
}
}
}
private void LoadWebView() {
wv.loadUrl(url);
}
private void initView() {
wv= (WebView) findViewById(R.id.wv);
}
}
主布局
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:layout_width="match_parent"
android:layout_height="match_parent">
listView布局
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_width="80dip"
android:layout_height="80dip"
/>
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:textSize="24dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:orientation="horizontal"
android:weightSum="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_weight="1"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />