伙计我在使用JSON从php服务器获取结果时遇到问题。基本上第一个活动运行良好,并通过PHP Server从Database获得适当的结果。第一项活动的结果是年数。
比用户选择年份并希望根据所选年份获得结果。
但是当选择年份时,从PHP文件和数据库获得的结果是错误的。它在第二个活动中获得了前一个结果年份,而不是图像的月份,路径和字节数组。
显示年度活动类:
public class DisplayYears extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> magazinesList;
private static final String TAG_SUCCESS = "success";
private static final String TAG_MAGAZINE = "magazines";
private static final String TAG_PID = "ID";
private static final String TAG_YEAR = "year";
JSONArray magazines = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_years);
magazinesList = new ArrayList<>();
new LoadAllYears().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int pyears = Integer.parseInt(((TextView) view.findViewById(R.id.years)).getText()
.toString());
Intent in = new Intent(getApplicationContext(),
DisplayMagazine.class);
in.putExtra("year", pyears);
startActivity(in);
}
});
}
@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 LoadAllYears extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DisplayYears.this);
pDialog.setMessage("Loading Years. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<>();
//String url_all_magazines = "http://172.16.26.190/shifaspeaks/get_all_years.php";
String url_all_magazines = "http://192.168.1.4/shifaspeaks/get_all_years.php";
JSONObject json = jParser.makeHttpRequest(url_all_magazines, "GET", params);
Log.d("All Magazines: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
magazines = json.getJSONArray(TAG_MAGAZINE);
for (int i = 0; i < magazines.length(); i++) {
JSONObject c = magazines.getJSONObject(i);
String year = c.getString(TAG_YEAR);
HashMap<String, String> map = new HashMap<>();
map.put(TAG_YEAR, year);
magazinesList.add(map);
}
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.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(
DisplayYears.this, magazinesList,
R.layout.list_item, new String[] { TAG_PID,
TAG_YEAR},
new int[] { R.id.id, R.id.years});
setListAdapter(adapter);
}
});
}
}
显示年份PHP文件:
<?php
mysql_connect('127.0.0.1:3306','root','');
mysql_select_db("shifaspeaks");
$response = array();
$result = mysql_query("SELECT DISTINCT year FROM magazine")
die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["magazines"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$magazine = array();
//$magazine["id"] = $row["ID"];
//$magazine["month"] = $row["month"];
$magazine["year"] = $row["year"];
//$magazine["path"] = $row["path"];
array_push($response["magazines"], $magazine);
}
$response["success"] = 1;
echo json_encode($response);
}
else {
$response["success"] = 0;
$response["message"] = "No magazines found";
echo json_encode($response);
}
?>
显示杂志活动类:
public class DisplayMagazine extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> magazinesList;
int year;
private static final String TAG_SUCCESS = "success";
private static final String TAG_MAGAZINE = "magazines";
private static final String TAG_MAGAZINE_IMAGE="magazineImage";
private static final String TAG_MONTH = "month";
private static final String TAG_PATH = "path";
JSONArray magazines = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_magazine);
Intent i=getIntent();
year=i.getIntExtra("year",0);
magazinesList = new ArrayList<>();
new LoadAllMagazines().execute();
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
@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 LoadAllMagazines extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DisplayMagazine.this);
pDialog.setMessage("Loading Magazines. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("year", String.valueOf(year)));
//String url_all_magazines = "http://172.16.26.190/shifaspeaks/get_all_magazines.php";
String url_all_magazines = "http://192.168.1.4/shifaspeaks/get_all_magazines.php";
JSONObject json = jParser.makeHttpRequest(url_all_magazines, "GET", params);
Log.d("All Magazines: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
magazines = json.getJSONArray(TAG_MAGAZINE);
for (int i = 0; i < magazines.length(); i++) {
JSONObject c = magazines.getJSONObject(i);
String month = c.getString(TAG_MONTH);
String path = c.getString(TAG_PATH);
String imageString=c.getString(TAG_MAGAZINE_IMAGE);
byte[] image= Base64.decode(imageString.getBytes(), Base64.DEFAULT);
Bitmap decodedByte= BitmapFactory.decodeByteArray(image,0,image.length);
HashMap<String, String> map = new HashMap<>();
map.put(TAG_MONTH, month);
map.put(TAG_PATH, path);
map.put(TAG_MAGAZINE_IMAGE,decodedByte.toString());
magazinesList.add(map);
}
} else {
Intent i = new Intent(getApplicationContext(),
MainActivity.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(
DisplayMagazine.this, magazinesList,
R.layout.list_item2, new String[] {TAG_MAGAZINE_IMAGE, TAG_MONTH,
TAG_PATH},
new int[] {R.id.magazineImage, R.id.month, R.id.path});
setListAdapter(adapter);
}
});
}
}
显示杂志PHP文件:
<?php
mysql_connect('127.0.0.1:3306','root','');
mysql_select_db("shifaspeaks");
$response = array();
if(isset($_GET["year"])){
$year = $_GET["year"];
$result = mysql_query("SELECT month, path, magazineImage FROM magazine where year='$year'") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["magazines"] = array();
while ($row = mysql_fetch_array($result)) {
$magazine = array();
$magazine["month"] = $row["month"];
$magazine["magazineImage"]=$row["magazineImage"];
$magazine["path"] = $row["path"];
array_push($response["magazines"], $magazine);
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No magazines found";
echo json_encode($response);
}
}
?>
请提前帮助我,谢谢你。
答案 0 :(得分:2)
您应该将listview和数组适配器作为类属性。我们必须通过适配器将数据从源(ArrayList)显示到列表视图。
在活动DisplayYears中:
public class DisplayYears extends ListActivity
{
...
private ArrayList<HashMap<String, String>> magazinesList;
private ArrayAdapter arrayAdapter; // Here you have to define the Adapter (ArrayAdapter, CursorAdapter, or a custom Adapter, etc ...)
private ListView lv;
...
在onCreate方法中,您必须告诉listview谁是将提供数据的适配器。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display_years);
// Init ArrayList
magazinesList = new ArrayList<>();
//Set Array Adapter with Array List
arrayAdapter = new ArrayAdapter(this, magazinesList);
// Get a reference to the ListView, and attach this adapter to it.
lv = (ListView) findViewById(R.id.listviewXmlLayout);
lv.setAdapter(arrayAdapter);
// Here lv.setOnItemClickListener implementation
// Start Request to Server
new LoadAllYears().execute();
}
这里的关键部分是来自LoadAllMagazines类的onPostExecute。在onPostExecute中,AsynTask线程与UI线程进行通信。
你应该从doInBackground方法返回一个Json,如下所示:
JSONObject doInBackground(String... args)
我们需要使用onPostExecute上从服务器接收的数据更新ArrayList(magazinesList)。 (不要再将内存分配给magazinesList,因为适配器(arrayAdapter)具有对magazinesList的内存引用,并且新的内存分配将破坏此关系)。
class LoadAllMagazines extends AsyncTask<String, String, JSONObject>
{
...
JSONObject doInBackground(String... args)
{
// Return Json received from server.
}
protected void onPostExecute(JSONObject file_url)
{
pDialog.dismiss();
// Parse the Json file
// Clear the ArrayList
magazinesList.clear();
// Load magazinesList with data received from server
// And the magic happens here! You only have to notify the adapter about the new changes in the ArrayList
arrayAdapter.notifyDataSetChanged();
// It is using observer pattern. Any changes in the ArrayList, we notify the Adapter and then it will tell the listView about the new changes.
};
}
请检查它是否适合你!