我将使用php,json从mysql数据库中显示listview android中的图像,但图像无法显示。 link_image应显示图像的位置,但不显示有效位置。请帮我。谢谢
status.php
<?php
// array for JSON response
$response = array();
// include file koneksi.php
include_once("connect.php");
$query = mysql_query("SELECT * FROM status order by id_status desc");
if (mysql_num_rows($query) > 0) {
$response["data_status"] = array();
while($baris = mysql_fetch_array ($query))
{
$id_status=$baris['id_status'];
$status=$baris['status'];
$id1=$baris['id_user'];
$id2=$baris['id_user_tujuan'];
$tanggal=$baris['tgl'];
$awalan = substr($id1, 0,2);
$awalan2 = substr($id2, 0,2);
switch($awalan)
{
case 'ad':
$name=mysql_query("SELECT * FROM admin where id_user = '$id1'");
// echo $nama;
while($baris2 = mysql_fetch_array ($name))
{
$nama2= $baris2['nama'];
}
break;
case 'al':
$name=mysql_query("SELECT * FROM alumni where id_user = '$id1'");
// echo $nama;
while($baris2 = mysql_fetch_array ($name))
{
$name2= $baris2['name'];
$link_image=$baris2['foto'];
$query_foto1 = mysql_query("SELECT foto FROM alumni where foto = '$link_image'");
$foto1 = mysql_fetch_array ($query_foto1);
if($foto1 == "belum upload")
{
$image = 'http://localhost/PortalAlumniSD/img/default.png';
}
else
{
$image = "http://localhost/PortalAlumniSD/photo/".$baris2['foto'];
}
}
break;
case 'pd':
$nama=mysql_query("SELECT * FROM admin where id_user = '$id1'");
// echo $nama;
while($baris2 = mysql_fetch_array ($nama))
{
$nama2= $baris2['nama'];
}
break;
case 'pl':
$nama=mysql_query("SELECT * FROM alumni where id_user = '$id1'");
// echo $nama;
while($baris2 = mysql_fetch_array ($nama))
{
$nama2= $baris2['nama'];
$link_image=$baris2['foto'];
$query_foto1 = mysql_query("SELECT foto FROM alumni where foto = '$link_image'");
$foto1 = mysql_fetch_array ($query_foto1);
if($foto1 == "belum upload")
{
$image = 'http://localhost/PortalAlumniSMP/img/default.png';
}
else
{
$image = "http://localhost/PortalAlumniSMP/photo/".$baris2['foto'];
}
}
break;
case 'md':
$nama=mysql_query("SELECT * FROM admin where id_user = '$id1'");
// echo $nama;
while($baris2 = mysql_fetch_array ($nama))
{
$nama2= $baris2['nama'];
}
break;
case 'ml':
$nama=mysql_query("SELECT * FROM alumni where id_user = '$id1'");
// echo $nama;
while($baris2 = mysql_fetch_array ($nama))
{
//$data_status["nama_alumni"] = stripslashes($baris2["nama"]);
$nama2= $baris2['nama'];
//$data_status["nama_alumni"] = $nama2;
$link_image=$baris2['foto'];
$query_foto1 = mysql_query("SELECT foto FROM alumni where foto = '$link_image'");
$foto1 = mysql_fetch_array ($query_foto1);
if($foto1 == "belum upload")
{
$image = 'http://localhost/PortalAlumniSMA/img/default.png';
}
else
{
$image = "http://localhost/PortalAlumniSMA/photo/".$baris2['foto'];
}
}
break;
}
switch($awalan2)
{
case 'ad':
$nama_tujuan=mysql_query("SELECT * FROM admin where id_user = '$id2'");
// echo $nama;
while($baris_nama1 = mysql_fetch_array ($nama_tujuan))
{
$nama3= $baris_nama1['nama'];
}
break;
case 'al':
$nama_tujuan=mysql_query("SELECT * FROM alumni where id_user = '$id2'");
// echo $nama;
while($baris_nama1 = mysql_fetch_array ($nama_tujuan))
{
//$nama3= $baris_nama1['nama'];
//$data_status["nama_alumni"] = $nama2;
$nama3= $baris_nama1['nama'];
//$data_status["nama_alumni_tujuan"] = $nama3;
$link_image=$baris_nama1['foto'];
$query_foto1 = mysql_query("SELECT foto FROM alumni where foto = '$link_image'");
if($foto1 == "belum upload")
{
$image = 'http://localhost/PortalAlumniSD/img/default.png';
}
else
{
$image = "http://localhost/PortalAlumniSD/photo/".$baris2['foto'];
}
}
break;
case 'pd':
$nama_tujuan=mysql_query("SELECT * FROM admin where id_user = '$id2'");
// echo $nama;
while($baris_nama1 = mysql_fetch_array ($nama_tujuan))
{
$nama3= $baris_nama1['nama'];
}
break;
case 'pl':
$nama_tujuan=mysql_query("SELECT * FROM alumni where id_user = '$id2'");
// echo $nama;
while($baris_nama1 = mysql_fetch_array ($nama_tujuan))
{
$nama3= $baris_nama1['nama'];
$link_image=$baris_nama1['foto'];
$query_foto1 = mysql_query("SELECT foto FROM alumni where foto = '$link_image'");
$foto1 = mysql_fetch_array ($query_foto1);
if($foto1 == "belum upload")
{
$image = 'http://localhost/PortalAlumniSMP/img/default.png';
}
else
{
$image = "http://localhost/PortalAlumniSMP/photo/".$baris2['foto'];
}
}
break;
case 'md':
$nama_tujuan=mysql_query("SELECT * FROM admin where id_user = '$id2'");
// echo $nama;
while($baris_nama1 = mysql_fetch_array ($nama_tujuan))
{
$nama3= $baris_nama1['nama'];
}
break;
case 'ml':
$nama_tujuan=mysql_query("SELECT * FROM alumni where id_user = '$id2'");
// echo $nama;
while($baris_nama1 = mysql_fetch_array ($nama_tujuan))
{
$nama3= $baris_nama1['nama'];
$link_image=$baris_nama1['foto'];
$query_foto1 = mysql_query("SELECT foto FROM alumni where foto = '$link_image'");
$foto1 = mysql_fetch_array ($query_foto1);
if($foto1 == "belum upload")
{
$image = 'http://localhost/PortalAlumniSMA/img/default.png';
}
else
{
$image = "http://localhost/PortalAlumniSMA/photo/".$baris2['foto'];
}
}
break;
}
$jum=mysql_query("SELECT * FROM komen where id_status='$id_status'");
$total= mysql_num_rows($jum);
if($id1==$id2){
$data_status["nama_alumni"] = $nama2;
$data_status["link_image"] = $image;
}else if($id1!=$id2){
$data_status["nama_alumni"] = $nama2;
$data_status["nama_alumni_tujuan"] = $nama3;
$data_status["link_image"] = $image;
}
// temp user array $data_status = array();
$data_status["id_user"] = $baris["id_user"];
$data_status["id_status"] = $baris["id_status"];
$data_status["id_user_tujuan"] = $baris["id_user_tujuan"];
$data_status["status"] = stripslashes($baris["status"]);
$data_status["date"] = stripslashes($baris["tgl"]);
$data_status["jml"] = $total;
// menampilkan hasil query dalam bentuk array
array_push($response["data_status"], $data_status);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}else {
// tidak ada data
$response["success"] = 0;
$response["message"] = "Tidak Ada Data";
// echoing JSON response
echo json_encode($response);
}
?>
显示json
{"data_status":[{"nama_alumni":"susa","link_image":"http:\/\/localhost\/PortalAlumniSMP\/photo\/","id_user":"plsusa@mail.com","id_status":"40","id_user_tujuan":"plsusa@mail.com","status":"ko","date":"April 2, 2014, 11:00 pm","jml":0,"nama_alumni_tujuan":"cumin"},{"nama_alumni":"susa","link_image":"http:\/\/localhost\/PortalAlumniSMP\/photo\/","id_user":"plsusa@mail.com","id_status":"36","id_user_tujuan":"plsusa@mail.com","status":"dfdsg","date":"April 2, 2014, 9:19 pm","jml":0,"nama_alumni_tujuan":"cumin"},{"nama_alumni":"cumin","link_image":"http:\/\/localhost\/PortalAlumniSD\/photo\/","id_user":"altest@mail.com","id_status":"35","id_user_tujuan":"alcandramarlina@ymail.com","status":"hhg","date":"April 2, 2014, 2:49 pm","jml":0,"nama_alumni_tujuan":"candra kasih"},{"nama_alumni":"cumin","link_image":"http:\/\/localhost\/PortalAlumniSD\/photo\/","id_user":"altest@mail.com","id_status":"33","id_user_tujuan":"alcandra.ratri@gmail.com","status":"","date":"April 2, 2014, 2:41 pm","jml":1,"nama_alumni_tujuan":"kasih"},{"nama_alumni":"cumin","link_image":"http:\/\/localhost\/PortalAlumniSD\/photo\/","id_user":"altest@mail.com","id_status":"29","id_user_tujuan":"alcandramarlina@ymail.com","status":"ayoo","date":"March 22, 2014, 6:49 am","jml":0,"nama_alumni_tujuan":"candra kasih"},{"nama_alumni":"kasih","link_image":"http:\/\/localhost\/PortalAlumniSD\/photo\/","id_user":"alcandra.ratri@gmail.com","id_status":"13","id_user_tujuan":"alcandra.ratri@gmail.com","status":"retertert","date":"March 5, 2014, 2:06 pm","jml":0,"nama_alumni_tujuan":"candra kasih"}],"success":1}
这个代码我在android中的应用程序:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.portalalumni.LoginActivity.UserLoginTask;
import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView mDrawerList;
private DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
private ArrayList<NsMenuItemModel> nsMenuItemModel;
SessionManager session;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> DataStatus;
// url
private static String url_data_status = "http://10.0.2.2/alumni/status.php";
// JSON Node names
public static final String TAG_SUCCESS = "success";
public static final String TAG_DATA_STATUS = "data_status";
public static final String TAG_ID_USER = "id_user";
public static final String TAG_ID_STATUS = "id_status";
public static final String TAG_ID_USER_TUJUAN = "id_user_tujuan";
public static final String TAG_DATE = "date";
public static final String TAG_NAMA_ALUMNI = "nama_alumni";
public static final String TAG_NAMA_ALUMNI_TUJUAN = "nama_alumni_tujuan";
//public static final String TAG_NAMA_ALUMNI_TUJUAN3 = "nama_alumni_tujuan";
public static final String TAG_STATUS = "status";
public static final String TAG_TOTAL = "jml";
public static final String TAG_LINK_IMAGE = "link_image";
public static final String TAG_LINK_IMAGE1 = "link_image1";
// daftar_rs JSONArray
JSONArray data_status = null;
ListView list;
String id_user,email,success;
ListStatus adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext());
Toast.makeText(getApplicationContext(),
"User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG)
.show();
session.checkLogin();
//Hashmap from login
HashMap<String, String> user = session.getUserDetails();
email = user.get(SessionManager.KEY_EMAIL);
id_user = user.get(SessionManager.KEY_IDUSER);
// Hashmap for ListView
DataStatus = new ArrayList<HashMap<String,String>>();
// Loading daftar rumah sakit in Background Thread
new LoadStatusActivity().execute();
// Get listview
list = (ListView) findViewById(R.id.list);
list.setAdapter(new ListStatus(this, DataStatus));
// React to user clicks on item
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
HashMap<String, String> selectedItem = DataStatus.get(position);
//To-DO get you data from the ItemDetails Getter
// selectedItem.get()
// the send the data using intent when opening another activity
Intent intent = new Intent(MainActivity.this, CommentActivity.class);
intent.putExtra("id_status",selectedItem.get(TAG_ID_STATUS));
startActivity(intent);
}
});
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
private void _initMenu() {
NsMenuAdapter mAdapter = new NsMenuAdapter(this);
// Add first block
menuItems = getResources().getStringArray(
R.array.menu);
String[] menuItemsIcon = getResources().getStringArray(
R.array.menu_items_icon);
nsMenuItemModel = new ArrayList<NsMenuItemModel>();
// adding nav drawer items to array
nsMenuItemModel.add(new NsMenuItemModel(menuItems[0], menuItemsIcon.length));
nsMenuItemModel.add(new NsMenuItemModel(menuItems[1], menuItemsIcon.length));
nsMenuItemModel.add(new NsMenuItemModel(menuItems[2], menuItemsIcon.length));
nsMenuItemModel.add(new NsMenuItemModel(menuItems[2], menuItemsIcon.length));
int res = 0;
for (String item : menuItems) {
int id_title = getResources().getIdentifier(item, "string",
this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon[res],
"drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
//if (res==1) mItem.counter=12; //it is just an example...
//if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mDrawerList = (ListView) findViewById(R.id.drawer);
if (mDrawerList != null)
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawer.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_search).setVisible(!drawerOpen);
menu.findItem(R.id.action_new_status).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
* The action bar home/up should open or close the drawer.
* ActionBarDrawerToggle will take care of this.
*/
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_search:
search();
return true;
case R.id.action_new_status:
newstatus();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void newstatus() {
// TODO Auto-generated method stub
Intent i = null;
i = new Intent(MainActivity.this, NewStatusActivity.class);
startActivity(i);
}
private void search() {
// TODO Auto-generated method stub
Intent i = null;
i = new Intent(MainActivity.this, SearchAlumniActivity.class);
startActivity(i);
}
private void profile() {
// TODO Auto-generated method stub
Intent i = null;
i = new Intent(MainActivity.this, ProfileActivity.class);
startActivity(i);
}
private void home() {
// TODO Auto-generated method stub
Intent i = null;
i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
private void inbox() {
// TODO Auto-generated method stub
Intent i = null;
i = new Intent(MainActivity.this, InboxActivity.class);
startActivity(i);
}
private void logout() {
// TODO Auto-generated method stub
session.logoutUser();
finish();
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {
public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
super(
mActivity,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.menu_open,
R.string.menu_close);
}
@Override
public void onDrawerClosed(View view) {
getActionBar().setTitle(getString(R.string.menu_close));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(getString(R.string.menu_open));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Highlight the selected item, update the title, and close the drawer
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
switch (position) {
case 0:
profile();
break;
case 1:
home();
break;
case 2:
inbox();
break;
case 3:
logout();
break;
default:
break;
}
mDrawer.closeDrawer(mDrawerList);
}
}
public static void unbindDrawables(View view) {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
try {
((ViewGroup) view).removeAllViews();
} catch (UnsupportedOperationException e) {
// No problem, nothing to do here
}
}
}
}
/*** Updating parsed JSON data into ListView * */
public void SetListViewAdapter(ArrayList<HashMap<String, String>> status) {
adapter = new ListStatus(this, status);
list.setAdapter(adapter);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode,data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted record
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/*** Background Async Task to Load all record data by making HTTP Request * */
class LoadStatusActivity extends AsyncTask<String, String, String> {
/*** Before starting background thread Show Progress Dialog * */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/*** getting All record data from url * */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_data_status, "GET", params);
// Check your log cat for JSON reponse
Log.d("Status: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Ada record Data (SUCCESS = 1)
// Getting Array of data_alumni
data_status = json.getJSONArray(TAG_DATA_STATUS);
// looping through All data_alumni
for (int i = 0; i < data_status.length(); i++) {
JSONObject c = data_status.getJSONObject(i);
// Storing each json item in variable
String id_user = c.getString(TAG_ID_USER);
String id_status = c.getString(TAG_ID_STATUS);
String id_user_tujuan = c.getString(TAG_ID_USER);
String date = c.getString(TAG_DATE);
String jml = c.getString(TAG_TOTAL);
String nama_alumni = c.getString(TAG_NAMA_ALUMNI);
String nama_alumni_tujuan = c.getString(TAG_NAMA_ALUMNI_TUJUAN);
//String nama_alumni_tujuan3 = c.getString(TAG_NAMA_ALUMNI_TUJUAN3);
String status = c.getString(TAG_STATUS);
String link_image = c.getString(TAG_LINK_IMAGE);
String link_image1 = c.getString(TAG_LINK_IMAGE1);
// creating new HashMap
HashMap<String, String> map = new HashMap<String,String>();
// adding each child node to HashMap key => value
map.put(TAG_ID_USER, id_user);
map.put(TAG_ID_STATUS, id_status);
map.put(TAG_ID_USER_TUJUAN, id_user_tujuan);
map.put(TAG_DATE, date);
map.put(TAG_TOTAL, jml);
map.put(TAG_NAMA_ALUMNI, nama_alumni);
map.put(TAG_NAMA_ALUMNI_TUJUAN, nama_alumni_tujuan);
//map.put(TAG_NAMA_ALUMNI_TUJUAN3, nama_alumni_tujuan3);
map.put(TAG_STATUS, status);
map.put(TAG_LINK_IMAGE, link_image);
map.put(TAG_LINK_IMAGE1, link_image1);
// adding HashList to ArrayList
DataStatus.add(map);
}
} else {
// Tidak Ada Record Data (SUCCESS = 0)
super.onCancelled();
}
} 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 record data alumni
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// updating listview
SetListViewAdapter(DataStatus);
}
});
}
}
/**@Override
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
} **/
}
答案 0 :(得分:0)
根据json的说法,这是您图片的网址:http://localhost/PortalAlumniSMP/photo/
。
它指的是localhost,因此它可能适用于您的PC(假设它也是您的Web服务器)。但它不适用于Android设备。
Localhost是指当前设备 - 即当您通过桌面浏览它时,它会在您的PC上查找图像(确实存在)。当您通过智能手机打开它时,它只在gsm上查找图像 - 而不是您的PC。因此找不到它。
您需要在某个地方发布您的Web应用程序 - 将其托管在您自己的Web服务器上或使用某些托管服务提供商/云服务。然后通过域名而不是“localhost”来查看图像。