我试图将我的android项目连接到php上的数据库我的代码中没有错误但是当我试图在手机上运行我的应用程序时它无法正常工作
public class Historical_Sites extends ListActivity{
private ProgressDialog pDialog;
Json_Parsing jParser = new Json_Parsing();
ArrayList<HashMap<String, String>> SitesList;
private static String url_all_Sites = "http://tourin.esy.es/php/historical.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_POSTS = "posts";
private static final String TAG_SITE_NO="Site_NO";
private static final String TAG_SITE_NAME = "Site_Name";
JSONArray Sites = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.historical_sites);
SitesList = new ArrayList<HashMap<String, String>>();
new LoadAllProducts().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sitname = ((TextView) view.findViewById(R.id.sitenam)).getText()
.toString();
Intent in = new Intent(getApplicationContext(),
Check.class);
in.putExtra(TAG_SITE_NAME, sitname);
startActivityForResult(in, 100);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 100) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
class LoadAllProducts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Historical_Sites.this);
pDialog.setMessage("Loading Historical Sites. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_Sites, "GET", params);
Log.d("All Sites: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Sites = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < Sites.length(); i++) {
JSONObject c = Sites.getJSONObject(i);
String sitno= c.getString(TAG_SITE_NO);
String Sitenam = c.getString(TAG_SITE_NAME);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SITE_NO, sitno);
map.put(TAG_SITE_NAME, Sitenam);
SitesList.add(map);
}
} else {
Intent i = new Intent(getApplicationContext(),
Chat.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
ListAdapter adapter = new SimpleAdapter(
Historical_Sites.this, SitesList,
R.layout.historical_sites_rows, new String[] {TAG_SITE_NAME},
new int[] { R.id.sitenam });
setListAdapter(adapter);
}
});
}
}
}
这是Json_Parsing
public class Json_Parsing {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public Json_Parsing() { }
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;
}
public JSONObject getJSONFromUrl(String url) {
try {
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 {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
/*
*
*
*
*
*
* */
}
我这是我在LogCat&#39;
中的错误E / ViewRootImpl:sendUserActionEvent()mView == null
答案 0 :(得分:3)
android:layout_gravity="center"