Java Android:从数据库获取数据会导致应用程序停止响应,卡在进度对话框中
嘿,伙计们,我正在创建一个应用程序,它用mysql中的数据填充列表视图,填充列表视图的数据包括courseid、courseName和讲师姓名。但是,当我单击按钮查看列表时,它会按应有的方式创建进度对话框,但是它会被卡住,然后应用程序停止响应 下面是我认为导致错误的代码,因为logcat提到了此类中的doInBackground: 日志cat文件是: 我真的很感谢你的时间和帮助,我还想对我的调试技能说声对不起,我仍然习惯于androidJava Android:从数据库获取数据会导致应用程序停止响应,卡在进度对话框中,java,android,Java,Android,嘿,伙计们,我正在创建一个应用程序,它用mysql中的数据填充列表视图,填充列表视图的数据包括courseid、courseName和讲师姓名。但是,当我单击按钮查看列表时,它会按应有的方式创建进度对话框,但是它会被卡住,然后应用程序停止响应 下面是我认为导致错误的代码,因为logcat提到了此类中的doInBackground: 日志cat文件是: 我真的很感谢你的时间和帮助,我还想对我的调试技能说声对不起,我仍然习惯于android public class AllCoursesActivi
public class AllCoursesActivity extends ListActivity {
//progress dialog
private ProgressDialog pDialog;
//create json parser object to understand the php files that were created
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> courseList;
//url to get all the product list
private static String url_all_courses = "http://10.0.0.2/get_all_courses.php";
//JSON node Names
private static final String TAG_SUCCESS = "success";
private static final String TAG_COURSES = "courses";
private static final String TAG_COURSEID = "courseid";
private static final String TAG_COURSENAME = "courseName";
//products JSON array
JSONArray courses =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allcourses);
//hashmap for listview
courseList = new ArrayList<HashMap<String, String>>();
//loading courses in background thread
new LoadAllCourses().execute();
//GET list view
ListView lv = getListView();
}
class LoadAllCourses extends AsyncTask<String, String, String>{
//before starting the background thread show some progress dialog
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(AllCoursesActivity.this);
pDialog.setMessage("Loading Courses. Please Wait");
pDialog.setCancelable(false);
pDialog.setIndeterminate(false);
pDialog.show();
}
//getting all products from the URL
@Override
protected String doInBackground(String... args) {
//building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
//Getting JSON String from URL
JSONObject json = jsonParser.makeHttpRequest(url_all_courses, "GET", params);
//check log cat for json response
Log.d("All Products: ", json.toString());
try {
//checking for success TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1){
//it means courses were found
//Getting Array of products
courses = json.getJSONArray(TAG_COURSES);
//looping through all products
for (int i = 0; i < courses.length(); i++){
JSONObject c = courses.getJSONObject(i);
//storing each JSON Item in the variable
String courseid = c.getString(TAG_COURSEID);
String coursename = c.getString(TAG_COURSENAME);
//creating new HASHMAP
HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to hashmap key => value
map.put(TAG_COURSEID, courseid);
map.put(TAG_COURSENAME, coursename);
//adding Hash list to array list
courseList.add(map);
}
}else {
//no courses found
//go back to dashboard
Intent i = new Intent(getApplicationContext(),MainScreenActivity.class);
//closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}catch(JSONException e){
e.printStackTrace();
}
return null;
}
//after completing background task Dismiss the progress dialog
protected void onPostExecute(String file_url){
//dismiss the dialog after getting all the courses
pDialog.dismiss();
//updating ui from background thread
runOnUiThread(new Runnable() {
@Override
public void run() {
//updating parsed JSon data into list view
ListAdapter adapter = new SimpleAdapter(AllCoursesActivity.this, courseList,
R.layout.listcourse, new String[]{TAG_COURSEID, TAG_COURSENAME},
new int[]{R.id.courseid, R.id.coursename});
//updating listview
setListAdapter(adapter);
}
});
}
}
公共类所有课程活动扩展ListActivity{
//进度对话框
私人对话;
//创建json解析器对象以理解创建的php文件
JSONParser JSONParser=新的JSONParser();
ArrayList courseList;
//获取所有产品列表的url
私有静态字符串url\u所有\u课程=”http://10.0.0.2/get_all_courses.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串TAG_COURSES=“COURSES”;
私有静态最终字符串标记_COURSEID=“COURSEID”;
私有静态最终字符串标记\u COURSENAME=“COURSENAME”;
//产品JSON数组
JSONArray课程=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.allcourses);
//listview的hashmap
courseList=新的ArrayList();
//在后台线程中加载课程
新建LoadAllCourses().execute();
//获取列表视图
ListView lv=getListView();
}
类LoadAllCourses扩展异步任务{
//在启动后台线程之前,显示一些进度对话框
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(AllCoursesActivity.this);
pDialog.setMessage(“正在加载课程,请稍候”);
pDialog.setCancelable(假);
pDialog.setUndeterminate(假);
pDialog.show();
}
//从URL获取所有产品
@凌驾
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jsonParser.makeHttpRequest(url_all_courses,“GET”,params);
//检查日志cat中的json响应
Log.d(“所有产品:,json.toString());
试一试{
//检查成功标签
int success=json.getInt(TAG_success);
如果(成功==1){
//这意味着课程被找到了
//获取一系列产品
courses=json.getJSONArray(TAG_courses);
//在所有产品中循环
对于(int i=0;ivalue
地图放置(TAG_COURSEID,COURSEID);
地图放置(TAG_COURSENAME,COURSENAME);
//将哈希列表添加到数组列表
课程列表。添加(地图);
}
}否则{
//找不到课程
//返回仪表板
Intent i=新的Intent(getApplicationContext(),MainScreenActivity.class);
//关闭以前的所有活动
i、 添加标志(意图、标志、活动、清除、顶部);
星触觉(i);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
//完成后台任务后,关闭“进度”对话框
受保护的void onPostExecute(字符串文件\u url){
//完成所有课程后关闭对话框
pDialog.disclose();
//从后台线程更新ui
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
//将解析的JSon数据更新到列表视图中
ListAdapter=new SimpleAdapter(所有课程活动。此,课程列表,
R.layout.listcourse,新字符串[]{TAG_COURSEID,TAG_COURSENAME},
新的int[]{R.id.courseid,R.id.coursename});
//更新列表视图
setListAdapter(适配器);
}
});
}
}
}
编辑:如果我没有包括我的JSONParser类,那么很抱歉
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
//function to get url
//by making post or get method
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
//making the http request
try {
//check for request method
if (method == "POST") {
//request method is post
//default http client
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 parse the string to json object
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(){
}
//函数获取url
//通过制作post或get方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、列表参数){
//发出http请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为post
//默认http客户端
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);
HttpEn
Log.d("All Products: ", json.toString());
Log.d("All Products: ", json.toString());
if (json != null){
Log.d("MyApp", "All Products: " + json.toString());
}
else{
Log.d("MyApp", "json is null ");
}