我使用 eclipse 编写 android 应用程序和 php 网站。AndroidShowEvent 页面的编码:
<?php
$dbhost = ''; //due to security problem, it will not show
$dbuser = ''; //due to security problem, it will not show
$dbpass = ''; //due to security problem, it will not show
mysql_select_db('u507295856_ems');
mysql_query("SET NAMES 'utf8'");
$sql=mysql_query("select * from event where Event_ID = '".$_REQUEST['eid']."'");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
$JSON = json_encode($output);
print ($JSON);
mysql_close();
?>
AndroidEvent页面的编码:
<?php
$dbhost = ''; //due to security problem, it will not show
$dbuser = ''; //due to security problem, it will not show
$dbpass = ''; //due to security problem, it will not show
$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db('u507295856_ems');
mysql_query("SET NAMES 'utf8'");
$sql=mysql_query("select * from event order by Event_StartDate DESC");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
$JSON = json_encode($output);
print ($JSON);
mysql_close();
?>
在 Android App 中,它有一页是使用 ListView 来显示事件名称。此活动名称为 Activity_List。在这个可以在 ListView 中显示事件名称的 Activity_List 类中。Activity_List 的编码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
setTitle("EMS");
tvBack = (TextView) findViewById(R.id.tvBack);
tvEng = (TextView) findViewById(R.id.tvEng);
tvCh = (TextView) findViewById(R.id.tvCh);
lstActivity = (ListView) findViewById(R.id.lstActivity);
tvBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_List.this, EMSActivity.class);
startActivity(i);
}
});
tvCh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_List.this, Activity_List.class);
startActivity(i);
}
});
loadJSONFromWebAsync();
}
public void loadJSONFromWebAsync() {
Thread t = new Thread() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http post
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.ems.cixx6.com/AndroidEvent.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "utf-8"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Toast.makeText(getBaseContext(), "change information losed",
Toast.LENGTH_LONG).show();
}
Activity_List.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Activity_List.this.parseJSONAndRefreshListView();
}
});
} catch (Exception e) {
Toast.makeText(getBaseContext(), "please check internet connection",
Toast.LENGTH_LONG).show();
}
}
};
t.run();
}
public void parseJSONAndRefreshListView() {
// paring data
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
int EventID = json_data.getInt("Event_ID");
String event_Name = json_data.getString("Event_Name");
myList.add(event_Name);
myEventIDList.add("" + EventID);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList);
lstActivity.setAdapter(adapter);
lstActivity.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_List.this,
Activity_Detail.class);
int selectedID = Integer.parseInt(myEventIDList.get(arg2));
Log.i("test", Integer.toString(selectedID));
i.putExtra("ID", selectedID);
startActivity(i);
}
});
} catch (JSONException e1) {
e1.printStackTrace();
Toast.makeText(getBaseContext(), "can not find information", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
当用户单击其中一个事件时,它将进入 Activity_Detail 类以查看事件详细信息。我想将选定的事件 id 发送到 AndroidShowEvent php 页面。之后,它连接到数据库以选择符合 Activity_Detail 类中显示数据的事件 ID 记录。Activity_Detail 页面是使用 textView 来显示数据的。Activity_Detail 的编码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
setTitle("EMS");
tvBack = (TextView) findViewById(R.id.tvBack);
tvEng = (TextView) findViewById(R.id.tvEng);
tvCh = (TextView) findViewById(R.id.tvCh);
tvEventName = (TextView) findViewById(R.id.tvEventName);
tvEventLocationID = (TextView) findViewById(R.id.tvEventLocation);
tvEventDate = (TextView) findViewById(R.id.tvEventDate);
tvEventTime = (TextView) findViewById(R.id.tvEventTime);
tvEventPhone = (TextView) findViewById(R.id.tvEventPhone);
tvSpeakerName = (TextView) findViewById(R.id.tvSpeakerName);
tvEventContent = (TextView) findViewById(R.id.tvEventContent);
imgEventBanner = (ImageView) findViewById(R.id.imgEventBanner);
BtnFacebook = (Button) findViewById(R.id.BtnFacebook);
BtnTwitter = (Button) findViewById(R.id.BtnTwitter);
tvEventLocationID.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_Detail.this, PlaceMap.class);
startActivity(i);
}
});
tvSpeakerName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_Detail.this, Speaker.class);
startActivity(i);
}
});
BtnFacebook.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
BtnTwitter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
tvBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_Detail.this, EMSActivity.class);
startActivity(i);
}
});
tvCh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Activity_Detail.this,
Activity_Detail.class);
startActivity(i);
}
});
EventID = this.getIntent().getIntExtra("ID", 0);
String event = Integer.toString(EventID);
Log.i("test", event);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("eid", event));
Log.i("test",String.valueOf(params));
loadJSONFromWebAsync(params);
}
public void loadJSONFromWebAsync(List<NameValuePair> pair) {
final List<NameValuePair> _pair = pair;
Thread t = new Thread() {
//ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// http post
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.ems.cixx6.com/AndroidShowEvent.php");
httppost.setEntity(new UrlEncodedFormEntity(_pair));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
// convert response to string
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "utf-8"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Toast.makeText(getBaseContext(), "change information losed",
Toast.LENGTH_LONG).show();
}
Activity_Detail.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Activity_Detail.this.parseJSONAndRefreshListView();
}
});
} catch (Exception e) {
Toast.makeText(getBaseContext(), "please check Internet connection",
Toast.LENGTH_LONG).show();
}
}
};
t.run();
}
public void parseJSONAndRefreshListView() {
// paring data
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
String Event_Name = json_data.getString("Event_Name");
String Event_Location = json_data.getString("Event_Location");
String Event_StartTime = json_data.getString("Event_StartTime");
String Event_EndTime = json_data.getString("Event_EndTime");
String Event_Phone = json_data.getString("Event_Phone");
String Event_StartDate = json_data.getString("Event_StartDate");
String Event_EndDate = json_data.getString("Event_EndDate");
String Event_Description = json_data
.getString("Event_Description");
String Event_Description2 = Event_Description.replace("<br>",
"\n");
String Event_Image = json_data.getString("Event_Image");
String Speaker_Name = json_data.getString("Speaker_Name");
tvEventName.setText(Event_Name);
tvEventDate.setText(Event_StartDate + " 至 " + Event_EndDate);
tvEventTime.setText(Event_StartTime + " 至 " + Event_EndTime);
tvEventPhone.setText(Event_Phone);
tvEventContent.setText(Event_Description2);
SpannableString contentUnderline = new SpannableString(
Event_Location);
contentUnderline.setSpan(new UnderlineSpan(), 0,
contentUnderline.length(), 0);
tvEventLocationID.setText(contentUnderline);
SpannableString contentUnderline2 = new SpannableString(
Speaker_Name);
contentUnderline2.setSpan(new UnderlineSpan(), 0,
contentUnderline2.length(), 0);
tvSpeakerName.setText(contentUnderline2);
Bitmap bitmap = DownloadImage("http://www.ems.cixx6.com/"
+ Event_Image);
imgEventBanner.setImageBitmap(bitmap);
}
} catch (JSONException e1) {
e1.printStackTrace();
Toast.makeText(getBaseContext(), "can not find information", Toast.LENGTH_LONG)
.show();
} catch (ParseException e1) {
e1.printStackTrace();
}
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
Toast.makeText(getBaseContext(), "please check Internet connection", Toast.LENGTH_LONG)
.show();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
Toast.makeText(getBaseContext(), "Internet connection was losed", Toast.LENGTH_LONG)
.show();
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
因此。只显示了一个 Toast。消息是“找不到信息”。其他数据未显示。
我的目的是在 Activity_Detail 类中显示合适的事件数据。我应该如何修改代码?
这个问题让我很困扰,请帮帮我!!!