嘿伙计我正在创建一个应用程序,它用mysql中的数据填充列表视图,填充列表视图的数据包括courseid,courseName和lecturerName。但是,当我单击按钮查看列表时,它会创建进度对话框,但它应该卡住,然后应用程序停止响应。
下面是我认为导致错误的代码,因为logcat提到了关于此类中的doInBackground的内容:
日志cat文件是:http://gyazo.com/950bcce9d14f267f495a4801434c6151
我真的很感谢你的时间和帮助,我还想说我很抱歉我的调试技巧我还是习惯了安卓。
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);
}
});
}
}
}
编辑:对不起,如果我没有包含我的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;
}
}
答案 0 :(得分:0)
你得到一个NullPointerException并且它可能发生在这里:
Log.d("All Products: ", json.toString());
搜索&#34;由&#34;在您的日志cat输出中。它说它是由于尝试在null对象上使用org.json.JSONObject.toString()引起的。添加一个检查以确保您的对象在使用之前不会为空。
答案 1 :(得分:0)
看起来你的JSON对象在这一行被设置为null:
Log.d("All Products: ", json.toString());
在记录之前为JsonObject添加空检查。
看起来我们为此使用了相同的教程,请参阅我稍微不同的工作实现:
以下是检查null:
的方法if (json != null){
Log.d("MyApp", "All Products: " + json.toString());
}
else{
Log.d("MyApp", "json is null ");
}
答案 2 :(得分:0)
嘿谢谢你的帮助,但我意识到问题所在。你们是对的,它无法返回任何东西,但这是因为它没有正确地连接到数据库。我使用了ip config中的ip地址,并且我没有正确链接php文件,例如:
http://192.xxx.xx.x/android_api/get_all_courses.php
以上解决了所有帮助和解决方案的问题