package json1.com.example.john.json_;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.http.client.HttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final String port = "http://service.ln263.com/live/getVideoClass";
private ProgressDialog dialog = null;
private static final int SUCCESS = 0;
private static final int ERROR = 1;
private static final int Exception = 2;
private ListView lv;
private String data;
private List<TvShow> tvShows = new ArrayList<TvShow>();
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
dialog.dismiss();
switch (msg.what) {
case SUCCESS:
try {
lv.setAdapter(new MyAdapter(tvShows,MainActivity.this));
} catch (Exception e) {
e.printStackTrace();
}
break;
case ERROR:
Toast.makeText(MainActivity.this,"没能正确连接",Toast.LENGTH_SHORT).show();
break;
case Exception:
Toast.makeText(MainActivity.this,"出现异常",Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
sendRequestWithHttpURLConnection();
}
private void sendRequestWithHttpURLConnection() {
//开启线程发起网络请求
dialog = new ProgressDialog(this);
dialog.setMessage("Loading....");
dialog.show();
new Thread(new Runnable() {
@Override
public void run() {
//
try {
String response = HttpUtil.sendHttpRequest(port);
parseJSONWithGSON(response);
Message msg = Message.obtain();
URL url = new URL(port);
msg.what = SUCCESS;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
Message msg = Message.obtain();
msg.what = Exception;
handler.sendMessage(msg);
}
}
;
}).start();
}
private void parseJSONWithGSON(String jsonData) {
try {
//int型时间
double qiantime;
double xiantime;
double houtime;
SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
String time;
String name;
String timenext;
Date date;
Date date1;
long timelong;
long timelong1;
TvShow tvShow;
//获得当前时间
String nowtime = sf.format(new Date());
//获取JSON对象
JSONObject jsonObject = new JSONObject(jsonData);
int state = jsonObject.getInt("flag");
// Log.d("aaa","1111");
//解析json数据
if (state == 100) {
JSONArray dataArray = jsonObject.getJSONArray("data");
JSONObject tempObject;
JSONObject tempObjectnext = null;
for (int i = 0; i < dataArray.length(); i++) {
tempObject = (JSONObject) dataArray.get(i);
JSONArray tvStation = tempObject.getJSONArray("list_ln263_video");
for (int j = 0; j < tvStation.length(); j++) {
tempObject = (JSONObject) tvStation.get(j);
JSONArray programArray = tempObject.getJSONArray("list_ln263_videocontent");
Log.d("aaa","dsd");
for (int k = 0; k < programArray.length(); k++) {
//得到name
tempObject = (JSONObject) programArray.get(k);
String bo = "";
name = (tempObject.getString("name"));
//得到时间
time = (tempObject.getString("live_date")).substring(6,19);
timelong = Long.parseLong(time);
date = new Date(timelong);
time = sf.format(date);
//转换成int类型的时间
xiantime=Integer.parseInt(nowtime.substring(0,2))+Integer.parseInt(nowtime.substring(3))*0.01;
qiantime=Integer.parseInt(time.substring(0,2))+Integer.parseInt(time.substring(3))*0.01;
//把时间对应项设置为正在直播
if (k<(programArray.length()-1)) {
tempObjectnext = (JSONObject) programArray.get(k + 1);
timenext = (tempObjectnext.getString("live_date")).substring(6,19);
timelong1=Long.parseLong(timenext);
date1=new Date(timelong1);
houtime=Integer.parseInt(sf.format(date1).substring(0,2))+
Integer.parseInt(sf.format(date1).substring(3))*0.01;
if (xiantime>=qiantime&&xiantime<houtime){
bo="正在播放";
}
}
//把三个对应的条目添加到bean类
tvShow = new TvShow(time,name,bo);
tvShows.add(tvShow);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package json1.com.example.john.json_;
/**
* Created by john on 2016/3/10.
*/
import java.util.Calendar;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
public LayoutInflater mInflater;
private List mList;
public MyAdapter(List<TvShow> list,Context context) { mList = list; mInflater = LayoutInflater.from(context); } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { Log.d("asd","3"); viewHolder = new ViewHolder(); convertView = mInflater.inflate(R.layout.tvshow_item,null); viewHolder.time = (TextView) convertView.findViewById(R.id.tv_date); viewHolder.content = (TextView) convertView.findViewById(R.id.tv_name); viewHolder.now= (TextView) convertView.findViewById(R.id.tv_now); convertView.setTag(viewHolder); } viewHolder = (ViewHolder) convertView.getTag(); TvShow bean = mList.get(position); viewHolder.time.setText(bean.name); viewHolder.content.setText(bean.date); viewHolder.now.setText(bean.now); return convertView; } class ViewHolder { public TextView time; public TextView content; public TextView now; }
}
“`
package json1.com.example.john.json_;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by john on 2016/3/9.
*/
public class HttpUtil {
public static String sendHttpRequest(String address){
HttpURLConnection connection=null;
try {
URL url=new URL(address);
connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod(“GET”);
connection.setReadTimeout(8000);
connection.setConnectTimeout(8000);
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream is=connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(is,”utf-8”));
StringBuilder response=new StringBuilder();
String line;
while ((line=reader.readLine())!=null){
response.append(line);
}
return response.toString();
} catch (Exception e) { e.printStackTrace(); return e.getMessage(); }finally { if (connection!=null){ connection.disconnect(); } } }
}
“