当时 在解析数据的时候 总是报空指针异常 原因是 由于开启了新的线程,2者单独工作,在获取json数据时 主线程也工作,也就是json数据获取还没结束 主线程便执行
JSONArray jsonArray = new JSONArray(jsonData); 导致 jsonData为空,造成空指针, 解决方法一 在json数据解析之前先判空 即可解决 但是用户体验不好
解决方法二 是 用正在加载效果,其实很简单,就是先创建 show 然后找一个地方关闭 dismiss()即可 ,所以只需要用 handler 和message 接受和获取消息,当获取完毕时 发送message消息 获取后 关闭 正在加载并显示数据 即可 !
代码如下
public class MainActivity extends Activity {
String jsonData = null;
Button bt;
TextView txt;
ProgressDialog dialog;
private Handler mhandler = new Handler(){
@Override
public void handleMessage(Message msg) { //用handler 来获取解析结束 并关闭 正在加载 和 显示数据
switch (msg.what) {
case 1:
dialog.dismiss();
txt.setText(jsonData);
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt= (TextView) findViewById(R.id.txt);
bt = (Button) findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(MainActivity.this, "正在加载课表...","请稍后..." ,true,true); //创建正在加载框
fun();
// try {
// JSONArray jsonArray = new JSONArray(jsonData); //解析json数据
// int count = jsonArray.length();
// for(int i = 0;i<count;i++){
// JSONObject item = jsonArray.getJSONObject(i);
//
// System.out.println(item.optString("content"));
// }
// } catch (JSONException e) {
// e.printStackTrace();
// }
if(jsonData!=null){
try {
// System.out.println(jsonData);
JSONArray jsonObject = new JSONObject(jsonData).getJSONObject("data").getJSONArray("list"); <span style="font-family: Arial, Helvetica, sans-serif;">//解析json数据</span>
// System.out.println(jsonObject.optString("msg"));
JSONObject js = jsonObject.getJSONObject(0);
System.out.println("------------------>"+js.optString("description")+"----"+js.optString("create_time"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
}
public void fun(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpGet myGet = new HttpGet(constant.urlArticleString);
try {
HttpResponse httpResponse = httpClient.execute(myGet);
if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
System.out.println("------------->请求成功"+httpResponse.getStatusLine().getStatusCode());
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
jsonData = convertStreamToString(inputStream);
Message msg = mhandler.obtainMessage(1);
mhandler.sendMessage(msg);
}
}
else {
System.out.println("--------->请求失败"+httpResponse.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),// ��ֹģ�����ϵ�����
512 * 1024);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
Log.e("DataProvier convertStreamToString", e.getLocalizedMessage(),
e);
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(sb.toString());
return sb.toString();
}
}