Android 使用AsynTask从JSON显示Listview
我在使用Android 使用AsynTask从JSON显示Listview,android,json,listview,Android,Json,Listview,我在使用asyntask从JSON显示listview时遇到了一些问题。我想在单击特定的单选按钮下显示数据,但DDMS结果显示“原因:java.lang.NullPointerException” listreult.java public class ListResult extends ListActivity implements OnClickListener { private ProgressDialog pDialog; String paramString; JSONParser
asyntask
从JSON
显示listview
时遇到了一些问题。我想在单击特定的单选按钮下显示数据,但DDMS结果显示“原因:java.lang.NullPointerException”
listreult.java
public class ListResult extends ListActivity implements OnClickListener {
private ProgressDialog pDialog;
String paramString;
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> busList;
RadioButton rbSUN;
RadioButton rbFRI;
RadioButton rbSAT;
ListView lv;
private int success;
private static String url_insert_new = "http://10.0.2.2/onlineLibrary/getallbus.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_IDIOMS = "idioms";
private static final String TAG_ID = "id";
private static final String TAG_TIME = "time";
private static final String TAG_STATUS = "status";
JSONArray idioms = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_result);
busList = new ArrayList<HashMap<String, String>>();
lv = getListView();
rbSUN = (RadioButton) findViewById(R.id.radioSUN);
rbFRI = (RadioButton) findViewById(R.id.radioFRI);
rbSAT = (RadioButton) findViewById(R.id.radioSAT);
rbSUN.setOnClickListener(this);
rbFRI.setOnClickListener(this);
rbSAT.setOnClickListener(this);
new InsertNewIdiom().execute();
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.radioSUN){
new InsertNewIdiom().execute();
if (success==1){
Toast.makeText(getApplicationContext(), "New item saved...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "New item FAILED to saved...", Toast.LENGTH_LONG).show();
}
paramString="SUN";
}
if (v.getId()==R.id.radioFRI){
new InsertNewIdiom().execute();
if (success==1){
Toast.makeText(getApplicationContext(), "New item saved...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "New item FAILED to saved...", Toast.LENGTH_LONG).show();
}
paramString="FRI";
}
if (v.getId()==R.id.radioSAT){
new InsertNewIdiom().execute();
if (success==1){
Toast.makeText(getApplicationContext(), "New item saved...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "New item FAILED to saved...", Toast.LENGTH_LONG).show();
}
paramString="SAT";
}
}
class InsertNewIdiom extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListResult.this);
pDialog.setMessage("Loading ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("time", TAG_TIME));
params.add(new BasicNameValuePair("status", TAG_STATUS));
JSONObject json = jsonParser.makeHttpRequest(url_insert_new,
"GET", params);
Log.d("Insert New item Response", json.toString());
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
idioms = json.getJSONArray(TAG_IDIOMS);
for (int i = 0; i < idioms.length(); i++) {
JSONObject c = idioms.getJSONObject(i);
String id = c.getString(TAG_ID);
String time = c.getString(TAG_TIME);
String status = c.getString(TAG_STATUS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TIME, time);
map.put(TAG_STATUS, status);
busList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
// pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
ListResult.this, busList,
R.layout.list_view, new String[]
{ TAG_ID, TAG_TIME, TAG_STATUS},
new int[] { R.id.id, R.id.time, R.id.status});
setListAdapter(adapter);
}
});
}
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
公共类ListResult扩展ListActivity实现OnClickListener{
私人对话;
字符串参数字符串;
JSONParser JSONParser=新的JSONParser();
ArrayList总线列表;
无线按钮rbSUN;
无线电按钮;
无线按钮卫星;
ListView lv;
个人成功;
私有静态字符串url_insert_new=”http://10.0.2.2/onlineLibrary/getallbus.php";
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_IDIOMS=“IDIOMS”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记\u TIME=“TIME”;
私有静态最终字符串标记_STATUS=“STATUS”;
JSONArray习语=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u result);
busList=newarraylist();
lv=getListView();
rbSUN=(RadioButton)findViewById(R.id.radioSUN);
rbFRI=(RadioButton)findViewById(R.id.radioFRI);
rbSAT=(RadioButton)findViewById(R.id.radioSAT);
rbSUN.setOnClickListener(this);
rbFRI.setOnClickListener(此);
rbSAT.setOnClickListener(这个);
新InsertNewIdiom().execute();
}
@凌驾
公共void onClick(视图v){
if(v.getId()==R.id.radioSUN){
新InsertNewIdiom().execute();
如果(成功==1){
Toast.makeText(getApplicationContext(),“新项目已保存…”,Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“新项目未能保存…”,Toast.LENGTH\u LONG.show();
}
paramString=“SUN”;
}
如果(v.getId()==R.id.radioFRI){
新InsertNewIdiom().execute();
如果(成功==1){
Toast.makeText(getApplicationContext(),“新项目已保存…”,Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“新项目未能保存…”,Toast.LENGTH\u LONG.show();
}
paramString=“FRI”;
}
if(v.getId()==R.id.radioSAT){
新InsertNewIdiom().execute();
如果(成功==1){
Toast.makeText(getApplicationContext(),“新项目已保存…”,Toast.LENGTH\u LONG.show();
}否则{
Toast.makeText(getApplicationContext(),“新项目未能保存…”,Toast.LENGTH\u LONG.show();
}
paramString=“SAT”;
}
}
类InsertNewSigmon扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(ListResult.this);
设置消息(“加载…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“时间”,标记时间));
参数添加(新的BasicNameValuePair(“状态”,标记状态));
JSONObject json=jsonParser.makeHttpRequest(url\u insert\u new,
“得到”,params);
Log.d(“插入新项响应”,json.toString());
试一试{
success=json.getInt(TAG_success);
如果(成功==1){
idioms=json.getJSONArray(TAG_-idioms);
for(inti=0;i
JsonParser.java
public class ListResult extends ListActivity implements OnClickListener {
private ProgressDialog pDialog;
String paramString;
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> busList;
RadioButton rbSUN;
RadioButton rbFRI;
RadioButton rbSAT;
ListView lv;
private int success;
private static String url_insert_new = "http://10.0.2.2/onlineLibrary/getallbus.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_IDIOMS = "idioms";
private static final String TAG_ID = "id";
private static final String TAG_TIME = "time";
private static final String TAG_STATUS = "status";
JSONArray idioms = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_result);
busList = new ArrayList<HashMap<String, String>>();
lv = getListView();
rbSUN = (RadioButton) findViewById(R.id.radioSUN);
rbFRI = (RadioButton) findViewById(R.id.radioFRI);
rbSAT = (RadioButton) findViewById(R.id.radioSAT);
rbSUN.setOnClickListener(this);
rbFRI.setOnClickListener(this);
rbSAT.setOnClickListener(this);
new InsertNewIdiom().execute();
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.radioSUN){
new InsertNewIdiom().execute();
if (success==1){
Toast.makeText(getApplicationContext(), "New item saved...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "New item FAILED to saved...", Toast.LENGTH_LONG).show();
}
paramString="SUN";
}
if (v.getId()==R.id.radioFRI){
new InsertNewIdiom().execute();
if (success==1){
Toast.makeText(getApplicationContext(), "New item saved...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "New item FAILED to saved...", Toast.LENGTH_LONG).show();
}
paramString="FRI";
}
if (v.getId()==R.id.radioSAT){
new InsertNewIdiom().execute();
if (success==1){
Toast.makeText(getApplicationContext(), "New item saved...", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "New item FAILED to saved...", Toast.LENGTH_LONG).show();
}
paramString="SAT";
}
}
class InsertNewIdiom extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ListResult.this);
pDialog.setMessage("Loading ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("time", TAG_TIME));
params.add(new BasicNameValuePair("status", TAG_STATUS));
JSONObject json = jsonParser.makeHttpRequest(url_insert_new,
"GET", params);
Log.d("Insert New item Response", json.toString());
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
idioms = json.getJSONArray(TAG_IDIOMS);
for (int i = 0; i < idioms.length(); i++) {
JSONObject c = idioms.getJSONObject(i);
String id = c.getString(TAG_ID);
String time = c.getString(TAG_TIME);
String status = c.getString(TAG_STATUS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, id);
map.put(TAG_TIME, time);
map.put(TAG_STATUS, status);
busList.add(map);
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
// pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
ListResult.this, busList,
R.layout.list_view, new String[]
{ TAG_ID, TAG_TIME, TAG_STATUS},
new int[] { R.id.id, R.id.time, R.id.status});
setListAdapter(adapter);
}
});
}
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
公共JSONParser(){
}
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
试一试{
如果(方法==“POST”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e)
<?php
$response=array();
$paramString=$_REQUEST[''];
require_once __DIR__.'/db_connect.php';
$db=new DB_CONNECT();
$sql="SELECT * FROM getbus WHERE day='$paramString'";
$result=mysql_query($sql);
if(!empty($result))
{
if(mysql_num_rows($result)>0)
{
$response['idioms']=array();
while ($row = mysql_fetch_array($result)) {
$book=array();
$book['day']=$row['day'];
$book['time']=$row['time'];
$book['status']=$row['status'];
array_push($response['idioms'], $book);
}
$response['success']=1;
echo json_encode($response);
}
else {
$response['success']=0;
$response['message']='No data found';
echo json_encode($response);
}
}
?>
02-07 14:37:09.761: E/JSON Parser(1195): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
02-07 14:37:19.091: E/JSON Parser(1195): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
getallbus.php?requestParam=[yourRequestParam] // aka `GET`
$paramString = $_GET['requestParam'];
$paramString = $_POST['requestParam'];