我正在尝试将我的项目连接到服务器上的数据库但它崩溃了,应用程序中没有错误但是当我在移动设备上运行它时它崩溃了,我已经添加了互联网权限
public class Chat extends ListActivity {
private ProgressDialog pDialog;
private static final String READ_COMMENTS_URL="http://tourin.esy.es/php/historical.php";
private static final String TAG_SITE_NAME = "Site_Name";
private static final String TAG_POSTS = "posts";
private JSONArray mComments = null;
private ArrayList<HashMap<String,String>> mCommentList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.historical_sites);
//setListAdapter();
}
@Override
protected void onResume() {
super.onResume();
new LoadSites().execute();
}
public class LoadSites extends AsyncTask<Void,Void,Boolean>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog= new ProgressDialog(Chat.this);
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected Boolean doInBackground(Void... arg0) {
updateJSONdata();
return null;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(Chat.this,mCommentList,R.layout.historical_sites_rows,
new String[]{TAG_SITE_NAME},new int[]{R.id.sitenam});
setListAdapter(adapter);
}
}
public void updateJSONdata(){
mCommentList = new ArrayList<HashMap<String, String>>();
Json_Parsing jParser = new Json_Parsing();
JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);
try {
mComments = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mComments.length(); i++) {
JSONObject c = mComments.getJSONObject(i);
String Site_Name = c.getString(TAG_SITE_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SITE_NAME, Site_Name);
mCommentList.add(map);
}
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.historical_sites_rows,mCommentList);
setListAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
这个Json解析器的代码
public class Json_Parsing {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public Json_Parsing() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
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 a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
}