JOSN解析数据---从网络

时间:2023-02-16 12:01:43
package com.qiao.android13_exe_json;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
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.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private ListView listView_main_title;
private LinearLayout layout_main_dian;

private int curPage = 1;
private String urlString = "http://192.168.125.118:8081/java/json.php?page=";
private boolean isButtom = false;
// private List<Map<String, String>> listResult = null;
private List<Map<String, String>> total = null;
private SimpleAdapter adapter = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView_main_title = (ListView) findViewById(R.id.listView_main_title);
layout_main_dian = (LinearLayout) findViewById(R.id.layout_main_dian);

total = new ArrayList<Map<String, String>>();
Log.i(TAG, "==total" + total);
adapter = new SimpleAdapter(this, total, R.layout.item_main_listview,
new String[] { "question" },
new int[] { R.id.textView_item_title });
listView_main_title.setAdapter(adapter);

new MyTask(this).execute(urlString);
listView_main_title.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (isButtom) {
layout_main_dian.setVisibility(View.VISIBLE);
} else {
layout_main_dian.setVisibility(View.GONE);
}
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
isButtom = (firstVisibleItem + visibleItemCount == totalItemCount);

}
});
}

class MyTask extends AsyncTask<String, Void, byte[]> {
private Context context;
private ProgressDialog dialog;

public MyTask(Context context) {
this.context = context;
dialog = new ProgressDialog(context);
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle("提示");
dialog.setMessage("数据加载中");
}

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}

@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}

@Override
protected byte[] doInBackground(String... params) {
BufferedInputStream bis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
InputStream is = connection.getInputStream();
int code = connection.getResponseCode();
if (code == 200) {
bis = new BufferedInputStream(is);
int c = 0;
byte[] buf = new byte[1024 * 8];
while ((c = bis.read(buf)) != -1) {
baos.write(buf, 0, c);
baos.flush();
}
return baos.toByteArray();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(byte[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);

if (result == null) {
Toast.makeText(context, "网络访问获取数据失败", Toast.LENGTH_SHORT)
.show();
} else {
List<Map<String, String>> listResult = getURLList(result);
// Log.i(TAG, "==listResult" + listResult);
total.addAll(listResult);
// Log.i(TAG, "==total" + total);
adapter.notifyDataSetChanged();
}
dialog.dismiss();
}
}

public void clickButton(View view) {
switch (view.getId()) {
case R.id.layout_main_dian:
layout_main_dian.setVisibility(View.GONE);
curPage++;
new MyTask(MainActivity.this).execute(urlString + curPage);
break;
default:
break;
}
}

public List<Map<String, String>> getURLList(byte[] b) {// 数据源来自于网络异步访问
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
String str = new String(b);
try {
JSONObject jsonObject1 = new JSONObject(str);
JSONArray jsonArray1 = jsonObject1.getJSONArray("result");
for (int i = 0; i < jsonArray1.length(); i++) {
Map<String, String> map = new HashMap<String, String>();
JSONObject jsonObject = jsonArray1.getJSONObject(i);
String vlaue = jsonObject.getString("question");
map.put("question", vlaue);
list.add(map);
}
// Log.i(TAG, "==list" + list);
return list;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

@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;
}

}