利用JsonObject解析Json数据

前端之家收集整理的这篇文章主要介绍了利用JsonObject解析Json数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. package json1.com.example.john.json_;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.widget.ListView;
  10. import android.widget.Toast;
  11.  
  12. import com.google.gson.Gson;
  13. import com.google.gson.reflect.TypeToken;
  14.  
  15. import org.apache.http.client.HttpClient;
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.io.BufferedReader;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;
  24. import java.lang.reflect.Type;
  25. import java.net.HttpURLConnection;
  26. import java.net.MalformedURLException;
  27. import java.net.ProtocolException;
  28. import java.net.URL;
  29. import java.text.SimpleDateFormat;
  30. import java.util.ArrayList;
  31. import java.util.Calendar;
  32. import java.util.Date;
  33. import java.util.List;
  34.  
  35. public class MainActivity extends AppCompatActivity {
  36.  
  37. private static final String port = "http://service.ln263.com/live/getVideoClass";
  38. private ProgressDialog dialog = null;
  39. private static final int SUCCESS = 0;
  40. private static final int ERROR = 1;
  41. private static final int Exception = 2;
  42. private ListView lv;
  43. private String data;
  44. private List<TvShow> tvShows = new ArrayList<TvShow>();
  45. private Handler handler = new Handler() {
  46. public void handleMessage(android.os.Message msg) {
  47. dialog.dismiss();
  48. switch (msg.what) {
  49. case SUCCESS:
  50. try {
  51. lv.setAdapter(new MyAdapter(tvShows,MainActivity.this));
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. break;
  56. case ERROR:
  57. Toast.makeText(MainActivity.this,"没能正确连接",Toast.LENGTH_SHORT).show();
  58. break;
  59. case Exception:
  60. Toast.makeText(MainActivity.this,"出现异常",Toast.LENGTH_SHORT).show();
  61. }
  62. }
  63. };
  64.  
  65. @Override
  66. protected void onCreate(Bundle savedInstanceState) {
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.activity_main);
  69.  
  70.  
  71. lv = (ListView) findViewById(R.id.lv);
  72. sendRequestWithHttpURLConnection();
  73. }
  74.  
  75. private void sendRequestWithHttpURLConnection() {
  76. //开启线程发起网络请求
  77. dialog = new ProgressDialog(this);
  78. dialog.setMessage("Loading....");
  79. dialog.show();
  80. new Thread(new Runnable() {
  81. @Override
  82. public void run() {
  83. //
  84. try {
  85. String response = HttpUtil.sendHttpRequest(port);
  86. parseJSONWithGSON(response);
  87. Message msg = Message.obtain();
  88. URL url = new URL(port);
  89. msg.what = SUCCESS;
  90. handler.sendMessage(msg);
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. Message msg = Message.obtain();
  94. msg.what = Exception;
  95. handler.sendMessage(msg);
  96. }
  97. }
  98.  
  99. ;
  100. }).start();
  101. }
  102.  
  103. private void parseJSONWithGSON(String jsonData) {
  104. try {
  105.  
  106. //int型时间
  107. double qiantime;
  108. double xiantime;
  109. double houtime;
  110.  
  111. SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
  112. String time;
  113. String name;
  114. String timenext;
  115. Date date;
  116. Date date1;
  117. long timelong;
  118. long timelong1;
  119. TvShow tvShow;
  120. //获得当前时间
  121. String nowtime = sf.format(new Date());
  122. //获取JSON对象
  123. JSONObject jsonObject = new JSONObject(jsonData);
  124. int state = jsonObject.getInt("flag");
  125. // Log.d("aaa","1111");
  126. //解析json数据
  127. if (state == 100) {
  128. JSONArray dataArray = jsonObject.getJSONArray("data");
  129. JSONObject tempObject;
  130. JSONObject tempObjectnext = null;
  131. for (int i = 0; i < dataArray.length(); i++) {
  132. tempObject = (JSONObject) dataArray.get(i);
  133. JSONArray tvStation = tempObject.getJSONArray("list_ln263_video");
  134. for (int j = 0; j < tvStation.length(); j++) {
  135. tempObject = (JSONObject) tvStation.get(j);
  136. JSONArray programArray = tempObject.getJSONArray("list_ln263_videocontent");
  137. Log.d("aaa","dsd");
  138. for (int k = 0; k < programArray.length(); k++) {
  139. //得到name
  140. tempObject = (JSONObject) programArray.get(k);
  141. String bo = "";
  142. name = (tempObject.getString("name"));
  143. //得到时间
  144. time = (tempObject.getString("live_date")).substring(6,19);
  145. timelong = Long.parseLong(time);
  146. date = new Date(timelong);
  147. time = sf.format(date);
  148. //转换成int类型的时间
  149. xiantime=Integer.parseInt(nowtime.substring(0,2))+Integer.parseInt(nowtime.substring(3))*0.01;
  150. qiantime=Integer.parseInt(time.substring(0,2))+Integer.parseInt(time.substring(3))*0.01;
  151. //把时间对应项设置为正在直播
  152. if (k<(programArray.length()-1)) {
  153. tempObjectnext = (JSONObject) programArray.get(k + 1);
  154. timenext = (tempObjectnext.getString("live_date")).substring(6,19);
  155. timelong1=Long.parseLong(timenext);
  156. date1=new Date(timelong1);
  157. houtime=Integer.parseInt(sf.format(date1).substring(0,2))+
  158. Integer.parseInt(sf.format(date1).substring(3))*0.01;
  159. if (xiantime>=qiantime&&xiantime<houtime){
  160. bo="正在播放";
  161. }
  162. }
  163. //把三个对应的条目添加到bean类
  164. tvShow = new TvShow(time,name,bo);
  165. tvShows.add(tvShow);
  166. }
  167. }
  168. }
  169. }
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. }
  173.  
  174.  
  175. }
  176. }

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;

  1. public MyAdapter(List<TvShow> list,Context context) {
  2. mList = list;
  3. mInflater = LayoutInflater.from(context);
  4. }
  5.  
  6. @Override
  7. public int getCount() {
  8.  
  9. return mList.size();
  10. }
  11.  
  12. @Override
  13. public Object getItem(int position) {
  14.  
  15. return mList.get(position);
  16. }
  17.  
  18. @Override
  19. public long getItemId(int position) {
  20.  
  21. return position;
  22. }
  23.  
  24. @Override
  25. public View getView(int position,View convertView,ViewGroup parent) {
  26. ViewHolder viewHolder = null;
  27. if (convertView == null) {
  28. Log.d("asd","3");
  29. viewHolder = new ViewHolder();
  30. convertView = mInflater.inflate(R.layout.tvshow_item,null);
  31. viewHolder.time = (TextView) convertView.findViewById(R.id.tv_date);
  32. viewHolder.content = (TextView) convertView.findViewById(R.id.tv_name);
  33. viewHolder.now= (TextView) convertView.findViewById(R.id.tv_now);
  34. convertView.setTag(viewHolder);
  35. }
  36. viewHolder = (ViewHolder) convertView.getTag();
  37. TvShow bean = mList.get(position);
  38. viewHolder.time.setText(bean.name);
  39. viewHolder.content.setText(bean.date);
  40. viewHolder.now.setText(bean.now);
  41.  
  42. return convertView;
  43. }
  44.  
  45. class ViewHolder {
  46. public TextView time;
  47. public TextView content;
  48. public TextView now;
  49. }

}

“`
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();

  1. } catch (Exception e) {
  2. e.printStackTrace();
  3. return e.getMessage();
  4. }finally {
  5. if (connection!=null){
  6. connection.disconnect();
  7. }
  8. }
  9. }

}

猜你在找的Json相关文章