我的毕设涉及的东西

项目结构

分析

adapter相关:

以活动模块的listview当参考



import java.util.List;
import java.util.Map;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.stgl.R;
import com.example.stgl.bean.HuoDong;
import com.example.stgl.bean.PlunInfo;


public class HdAdapter extends BaseAdapter {


private List<HuoDong> list;
private LayoutInflater inflater;
//private static LayoutInflater inflater = null;



public HdAdapter(Context context,List<HuoDong> duanZiInfos)
{
inflater = LayoutInflater.from(context);
this.list = duanZiInfos;
}




@Override
public int getCount()
{
return list.size();

}


@Override
public Object getItem(int position)
{


return list.get(position);
}




@Override
public long getItemId(int position)
{

return position;
}




@Override
public View getView(int position, View convertView, ViewGroup parent)
{

ViewHolder holder = null;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.item_hd, null);
holder = new ViewHolder();
holder.text_title = (TextView) convertView.findViewById(R.id.txt_htitle);
holder.text_huodong = (TextView) convertView.findViewById(R.id.txt_hword);
holder.text_clue = (TextView) convertView.findViewById(R.id.hd_clue);
holder.text_time = (TextView) convertView.findViewById(R.id.hd_time);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.text_title.setText(list.get(position).getHtitle().toString());
holder.text_huodong.setText(list.get(position).getHword().toString());
holder.text_clue.setText(list.get(position).getOfclue().toString());
holder.text_time.setText(list.get(position).getHtime().toString());
return convertView;
}
class ViewHolder
{
private TextView text_title;
private TextView text_huodong;
private TextView text_clue;
private TextView text_time;
}
}

bean省略,json解析省略


网络数据请求和接收返回数据方式


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;


public class HttpUtils {


//public static String URL = "http://192.168.1.102:8080/STGLserver/login";
/*
     * Function  :   发送Post请求到服务器
     * Param     :   请求体内容,encode编码格式
 
     */
    public static String submitPostData(Map<String, String> params, String encode,String ul) {
        
        byte[] data = getRequestData(params, encode).toString().getBytes();//获得请求体
        try {            
        URL url = new URL(ul);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setConnectTimeout(3000);        //设置连接超时时间
            httpURLConnection.setDoInput(true);                  //打开输入流,以便从服务器获取数据
            httpURLConnection.setDoOutput(true);                 //打开输出流,以便向服务器提交数据
            httpURLConnection.setRequestMethod("POST");   //设置以Post方式提交数据
            httpURLConnection.setUseCaches(false);               //使用Post方式不能使用缓存
            //设置请求体的类型是文本类型
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //设置请求体的长度
            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
            //获得输出流,向服务器写入数据
            OutputStream outputStream = httpURLConnection.getOutputStream();
            outputStream.write(data);
            
            int response = httpURLConnection.getResponseCode();            //获得服务器的响应码
            if(response == HttpURLConnection.HTTP_OK) {
                InputStream inptStream = httpURLConnection.getInputStream();
                return dealResponseResult(inptStream);                     //处理服务器的响应结果
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    
    /*
     * Function  :   封装请求体信息
     * Param     :   请求体内容,encode编码格式
    
     */
    public static StringBuffer getRequestData(Map<String, String> params, String encode) {
        StringBuffer stringBuffer = new StringBuffer();        //存储封装好的请求体信息
        try {
            for(Map.Entry<String, String> entry : params.entrySet()) {
                stringBuffer.append(entry.getKey())
                            .append("=")
                            .append(URLEncoder.encode(entry.getValue(), encode))
                            .append("&");
            }
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);    //删除最后的一个"&"
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer;
    }
    /*
     * Function  :   处理服务器的响应结果(将输入流转化成字符串)
     * Param     :   服务器的响应输入流
     */
    public static String dealResponseResult(InputStream inputStream) {
        String resultData = null;      //存储处理结果
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        try {
            while((len = inputStream.read(data)) != -1) {
                byteArrayOutputStream.write(data, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        resultData = new String(byteArrayOutputStream.toByteArray());    
        return resultData;
    }
    
}

SharedPreferences 的使用

SharedPreferences sp = getSharedPreferences("usertext",Activity.MODE_PRIVATE);
Editor ed = sp.edit();
ed.putString("hid",Integer.toString(hid));
//ed.putString("rid", Integer.toString(rid));
ed.putString("hname", hname);
ed.commit();


activity处理数据之类相关的东西

// Runnable实现  
    Runnable mRunnable = new Runnable() { 
        @Override 
        public void run() { 
           
                try { 
                    Thread.sleep(1000);// 暂停2秒再发送消息  
              
                    getData();
                  
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
           // } 
        } 
    }; 
public void getData()
{
Map<String,String> map = new HashMap<String,String>();

map.put("school", school);

String aa = HttpUtils.submitPostData(map, "utf-8",UrlUtil.url_hdong);
Log.e(TAG, "-------------------------"+aa);
List<HuoDong> hdong = HdongUtil
.huodong(aa);
// android多线程规定:子线程中不可以更改UI
// 从系统的消息池中拿消息,节省资源
Message msg = Message.obtain();
msg.obj = hdong;
msg.what = 2;
handler_hd.sendMessage(msg);

}


Handler handler_hd = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 2:


@SuppressWarnings("unchecked")
List<HuoDong> hdong = (List<HuoDong>) msg.obj;
contentListView.setAdapter(new HdAdapter(HDongActivity.this, hdong));
break;


}
};
};

class HdAsyncTask extends AsyncTask<String, Void, List<HuoDong>>
{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressbar.setVisibility(View.VISIBLE);
}


@Override
protected List<HuoDong> doInBackground(String... arg) {
// TODO Auto-generated method stub
String url = arg[0];
Map<String,String> map = new HashMap<String,String>();
map.put("school", school);
String aa = HttpUtils.submitPostData(map, "utf-8",url);
List<HuoDong> hdong = HdongUtil
.huodong(aa);
return hdong;
}
@Override
protected void onPostExecute(List<HuoDong> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressbar.setVisibility(View.GONE);
contentListView.setAdapter(new HdAdapter(HDongActivity.this, result));

}

}


写的很乱,主要是反省一下,看起来这东西真的太简单了,竟然花了一个多月,弱


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值