Android简单物流查询

  新手学习Android 的一些记录!利用快递100 留出来的API接口去获取物流信息,然后利用Android上的Gson解析 json数据,利用ListView显示到App上面,只是一个简单的界面

没什么技术含量

程序的结构目录   一个Model  里面对应着Jons数据中的成员

 

然后生成一些Get  Set方法

 

还用定义一个Htpp类 用来获取网上的数据  最后返回字符串  在网上获取的应该是InputStream类型的,为了省事,我声明了一个工具类   放在了IOutils 包下面

一个静态方法,功能就是把InputStream装换成String  就是在网上获取Json数据转换成字符串,然后在让Gson去解析!

 

这个App主要拥有两个Activity   一个是让用户输入 快递公司,另一个是输入块单号!  在mainActivity启动然后携带参数跳转到第二个Activity中 在第二个Activity中有一个TextView 和一个LisView   

 

<strong>这个是mainActivity中的代码:</strong>

 



package com.example.logistics;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import com.example.gsonModel.DetailData;
import com.example.gsonModel.MyLogistics;
import com.example.httpUtil.HttpCallbackListener;
import com.example.httpUtil.HttpUtil;
import com.google.gson.Gson;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	

	private Button btn_find;
	private TextView ev_kd;
	private TextView ev_dh;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ev_kd = (TextView)findViewById(R.id.ev_kd);
		ev_dh = (TextView)findViewById(R.id.ev_dh);
		
		btn_find = (Button)findViewById(R.id.btn_find);
		btn_find.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				Bundle bundle=new Bundle();
				bundle.putString("kdName", ev_kd.getText().toString().trim());
				bundle.putString("urlAddress",ev_dh.getText().toString().trim());
				intent.putExtra("message",bundle);
				intent.setClass(MainActivity.this,LogisticActivity.class);
				startActivity(intent);
			}
		});
	}

}
下面这个是我用来显示具体物流信息的那个Activity代码
package com.example.logistics;


import java.util.List;
import com.example.gsonModel.DetailData;
import com.example.gsonModel.MyLogistics;
import com.example.httpUtil.HttpCallbackListener;
import com.example.httpUtil.HttpUtil;
import com.google.gson.Gson;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class LogisticActivity extends Activity {


	private Handler handler;
	private ListView lv_show;
	private String txt;
	private String kdName;
	private String kdNum;
	private TextView tv_message;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logistics);
		Intent intent=getIntent();
		Bundle bundle=intent.getBundleExtra("message");
		
		
		lv_show=(ListView)findViewById(R.id.lv_show);
		tv_message=(TextView)findViewById(R.id.tv_message);
		kdName=bundle.getString("kdName");
		kdNum=bundle.getString("urlAddress");
		HttpUtil.sendOkHttpRequest("http://www.kuaidi100.com/query?type="+kdName+"&postid="+kdNum,new HttpCallbackListener() {
					public void onFinish(String response) {
						// TODO Auto-generated method stub
						// 在这里根据返回内容执行具体逻辑
						String text = response.toString();
						Message msg = new Message();
						Bundle data = new Bundle();
						data.putString("logcontent",text);
						msg.setData(data);
						handler.sendMessage(msg);
					}


					@Override
					public void onError(Exception e) {
						// TODO Auto-generated method stub
						// 在这里对异常进行处理
						System.out.println(e);


					}
				});
		handler = new Handler() {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Bundle data = msg.getData();
				txt = data.getString("logcontent");
				Gson gson = new Gson();
				MyLogistics logistics = gson.fromJson(txt, MyLogistics.class);
				String nu = logistics.getNu();
				tv_message.setText("快递单号:"+nu+"   下面是详细物流信息");
				List<DetailData> datalists = logistics.getData();
				MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.logis_item,
				datalists);
				lv_show.setAdapter(adapter);
				
			}
		};
		
	


		
	}
	
	public class MyAdapter extends ArrayAdapter<DetailData> {
		private int resource;
		private Context context;
		public MyAdapter(Context context, int resource, List<DetailData> data) {
			super(context, resource, data);
			// TODO Auto-generated constructor stub
			this.resource = resource;
			this.context=context;


		}


		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			DetailData mess = getItem(position);
			if (convertView == null) {
				view = LayoutInflater.from(context).inflate(
						resource, parent, false);
			} else {
				view = convertView;
			}


			TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
			TextView tv_ftime = (TextView) view.findViewById(R.id.tv_ftime);
			TextView tv_context = (TextView) view.findViewById(R.id.tv_context);
			TextView tv_location = (TextView) view
					.findViewById(R.id.tv_location);


			tv_time.setText("到达时间:" + mess.getTime());
			tv_ftime.setText("发出时间:" + mess.getFtime());
			tv_context.setText("物流信息:" + mess.getContext());
			tv_location.setText("所在位置:" + mess.getLocation());
			return view;
		}


	}






}
private Handler handler;
	private ListView lv_show;
	private String txt;
	private String kdName;
	private String kdNum;
	private TextView tv_message;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_logistics);
		Intent intent=getIntent();
		Bundle bundle=intent.getBundleExtra("message");
		
		
		lv_show=(ListView)findViewById(R.id.lv_show);
		tv_message=(TextView)findViewById(R.id.tv_message);
		kdName=bundle.getString("kdName");
		kdNum=bundle.getString("urlAddress");
		HttpUtil.sendOkHttpRequest("http://www.kuaidi100.com/query?type="+kdName+"&postid="+kdNum,new HttpCallbackListener() {
					public void onFinish(String response) {
						// TODO Auto-generated method stub
						// 在这里根据返回内容执行具体逻辑
						String text = response.toString();
						Message msg = new Message();
						Bundle data = new Bundle();
						data.putString("logcontent",text);
						msg.setData(data);
						handler.sendMessage(msg);
					}


					@Override
					public void onError(Exception e) {
						// TODO Auto-generated method stub
						// 在这里对异常进行处理
						System.out.println(e);


					}
				});
		handler = new Handler() {
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				Bundle data = msg.getData();
				txt = data.getString("logcontent");
				Gson gson = new Gson();
				MyLogistics logistics = gson.fromJson(txt, MyLogistics.class);
				String nu = logistics.getNu();
				tv_message.setText("快递单号:"+nu+"   下面是详细物流信息");
				List<DetailData> datalists = logistics.getData();
				MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.logis_item,
				datalists);
				lv_show.setAdapter(adapter);
				
			}
		};
		
	


		
	}
	
	public class MyAdapter extends ArrayAdapter<DetailData> {
		private int resource;
		private Context context;
		public MyAdapter(Context context, int resource, List<DetailData> data) {
			super(context, resource, data);
			// TODO Auto-generated constructor stub
			this.resource = resource;
			this.context=context;


		}


		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			DetailData mess = getItem(position);
			if (convertView == null) {
				view = LayoutInflater.from(context).inflate(
						resource, parent, false);
			} else {
				view = convertView;
			}


			TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
			TextView tv_ftime = (TextView) view.findViewById(R.id.tv_ftime);
			TextView tv_context = (TextView) view.findViewById(R.id.tv_context);
			TextView tv_location = (TextView) view
					.findViewById(R.id.tv_location);


			tv_time.setText("到达时间:" + mess.getTime());
			tv_ftime.setText("发出时间:" + mess.getFtime());
			tv_context.setText("物流信息:" + mess.getContext());
			tv_location.setText("所在位置:" + mess.getLocation());
			return view;
		}


	}






}

 

 

联网工具类的代码

 

package com.example.httpUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.example.IOutils.InstreamToString;

import okhttp3.OkHttpClient;
import okhttp3.Request;

public class HttpUtil {
	public static void sendOkHttpRequest(final String address,final HttpCallbackListener listener )
	{
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				HttpURLConnection connection=null;
				
				try {
					URL url=new URL(address);
					connection=(HttpURLConnection) url.openConnection();
					connection.setRequestMethod("GET");
					connection.setConnectTimeout(8000);
					connection.setReadTimeout(8000);
					connection.setDoInput(true);
					connection.setDoOutput(true);
					InputStream in=connection.getInputStream();
					String text = InstreamToString.convertStreamToString(in);
					//System.out.println(text);
					if(listener!=null)
					{
						//回调onFinish()方法
						listener.onFinish(text);
						
					}
				} catch (Exception e) {
					if(listener!=null)
					{
						//回调onError()方法法
						listener.onError(e);
					}
					e.printStackTrace();
				}finally{
					if(connection!=null)
					{
						connection.disconnect();
					}
				}
				
			}
		}).start();
	
	}

}


HttpCallbackListener  回调代码

 

 

package com.example.httpUtil;

public interface HttpCallbackListener {
	void onFinish(String response);
	void onError(Exception e);
}


转换成字符串类的  代码

 

 

package com.example.IOutils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InstreamToString {

	 public static String convertStreamToString(InputStream is) {
		 BufferedReader reader=new BufferedReader(new InputStreamReader(is));
		 StringBuilder sb=new StringBuilder();
		 String line=null;
		 try {
			while((line=reader.readLine())!=null)
			 {
				 sb.append(line);
			 }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return sb.toString(); 
		 
	 }
	

}

 

 

 

 

 

 

 

效果图片,最坑的是我在设置ListView适配器的时候!!!啊啊,程序老是异常结束,各种调试,,,各种设置适配器参数,,最后结果发现,,,是我悲剧的没有

找到ListView这个组件,,,,,ListView lv_show=(ListView)findViewById(R.id.lv_show);   这次记住了!!

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值