爱问知识人 爱问教育 医院库

基于PHP后台的Android新闻浏览客户端,php后台android新闻_PHP教程

首页

基于PHP后台的Android新闻浏览客户端,php后台android新闻_PHP教程


        

提交回答
好评回答
  • 2023-03-15 03:30:05
      基于PHP后台的Android新闻浏览客户端,php后台android新闻本文实例为大家分享了Android新闻浏览客户端,基于php后台,供大家参考,具体内容如下1、使用HBuilder进行PHP环境配置,测试是否可以查询MySQL语句,之前都已经详细说明过了。
      2、此处php后台实现mysql的查询功能,并以JSON数据格式返回个客户端在PHP此处建立一个mysql_connect。php文件,实现数据库的连接,并设置字符集格式。然后新建一个getNewsJSON。php文件用于进行将查询结果转换成JSON字符串格式。
      只需要 json_encode这个方法即可。$row['title'],"desc"=>$row['desc'],"time"=>$row['time'],"content_url"=>$row['content_url'],"pic_url"=>$row['pic_url']);}//数组转化为JSON字符串echo json_encode($arr);?>重点在于Android端的设计开发1、设计界面由于需要以在ListView的每个Item中设置相同的格式,所以此处运用ListView+Adapter的形式在主界面LinearLayout中添加一个ListView控件2、Mainactivity程序如下:public class MainActivity extends Activity implements OnItemClickListener{ private ListView lvNews ; private NewsAdapter adapter ; //定义集合 private ListnewsList ; //获取json字符串的URL地址 public static final String GET_NEWS_URL = "http://211。
      87。234。20/NewsDemo/getNewsJSON。php"; //获取msg之后如何处理 private Handler getNewsHandler = new Handler(){ public void handleMessage(android。
      os。Message msg){ String jsonData = (String) msg。obj ; System。out。println(jsonData) ; try {JSONArray jsonArray = new JSONArray(jsonData) ;for(int i=0;i(); adapter = new NewsAdapter(this,newsList) ; lvNews。
      setAdapter(adapter) ; lvNews。setOnItemClickListener(this) ; HttpUtils。getNewsJSON(GET_NEWS_URL,getNewsHandler) ; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present。
       getMenuInflater()。inflate(R。menu。main, menu); return true; }@Overridepublic void onItemClick(AdapterViewarg0, View arg1, int position, long arg3) {// TODO Auto-generated method stubNews news = newsList。
      get(position) ;Intent intent = new Intent(this,BrowseNewsActivity。class) ;intent。putExtra("content_url",news。getContent_url()) ;startActivity(intent) ;} }此处需要一个工具类HttpUtils以及自定义的NewsAdapter以实现item的视图显示。
      HttpUtils代码如下:package com。MR。news。utils;import java。io。BufferedReader;import java。io。InputStream;import java。io。InputStreamReader;import java。
      net。HttpURLConnection;import java。net。URL;import android。graphics。Bitmap;import android。graphics。BitmapFactory;import android。
      os。Handler;import android。os。Message;import android。widget。ImageView;public class HttpUtils {//工具类直接定义成静态方法即可/*url用于内部类中,所以要将其设定为final类型*//*读取完成需要通知主线程,需要使用handler*/public static void getNewsJSON(final String url,final Handler handler){//访问网络,时间长,开启新线程new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubHttpURLConnection conn ;InputStream is ;try {conn = (HttpURLConnection) new URL(url)。
      openConnection() ;//GET方式获取conn。setRequestMethod("GET") ;//得到输入流is=conn。getInputStream() ;//读取数据用缓冲,里面要传入一个readerBufferedReader reader = new BufferedReader(new InputStreamReader(is));//一行一行读取数据String line = "";//没读完一行进行拼接,高效StringBuilder result = new StringBuilder();while((line = reader。
      readLine()) != null){result。append(line);}Message msg = new Message() ;//msg。obj可以放进去任何对象msg。obj = result。toString() ;handler。
      sendMessage(msg) ;} catch (Exception e) {e。printStackTrace();}}})。start() ;}public static void setPicBitMap(final ImageView ivPic,final String pic_url){new Thread(new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubtry {HttpURLConnection conn = (HttpURLConnection) new URL(pic_url)。
      openConnection() ;conn。connect() ;InputStream is = conn。getInputStream() ;//bitmap就是所需图片资源/*从资源文件中的到图片*/Bitmap bitmap = BitmapFactory。
      decodeStream(is) ;ivPic。setImageBitmap(bitmap) ;is。close() ;} catch (Exception e) {// TODO Auto-generated catch blocke。printStackTrace();} }})。
      start() ;}}NewsAdapter代码如下:package com。MR。news。adapter;import java。util。List;import com。MR。news。R;import com。MR。news。model。
      News;import com。MR。news。utils。HttpUtils;import android。content。Context;import android。view。LayoutInflater;import android。
      view。View;import android。view。ViewGroup;import android。widget。BaseAdapter;import android。widget。ImageView;import android。
      widget。TextView;public class NewsAdapter extends BaseAdapter {//声明上下文对象,后面的getView方法需要private Context context;private ListnewsList;public NewsAdapter(Context context, ListnewsList){this。
      context = context ;this。newsList = newsList ;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn newsList。
      size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn newsList。get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup arg2) {// TODO Auto-generated method stubif(convertView == null){convertView = LayoutInflater。
      from(context)。inflate(R。layout。news_item,null) ;}TextView tvTitle = (TextView) convertView。findViewById(R。id。tvTitle) ;TextView tvDesc = (TextView) convertView。
      findViewById(R。id。tvDesc) ;TextView tvTime = (TextView) convertView。findViewById(R。id。tvTime) ;ImageView ivPic = (ImageView) convertView。
      findViewById(R。id。ivPic);News news = newsList。get(position) ;tvTitle。setText(news。getTitle()) ;tvDesc。setText(news。getDesc()) ;tvTime。
      setText(news。getTime()) ;String pic_url = news。getPic_url() ;HttpUtils。setPicBitMap(ivPic, pic_url) ;return convertView;}}news_item用来设置每个item的显示格式注意:此item中需要显示单个图片,所以用到Bitmap这个类。
      由于用到网络传输,所以需要用到线程这个概念!关键理解handler message以及loop这三者的关系。

    累***

    2023-03-15 03:30:05

类似问题

换一换
  • 电脑/网络 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...
最新资料 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):