Android 应用程序:如何从网络服务获取图像以在应用程序中显示?

发布于 2024-12-24 01:42:10 字数 4054 浏览 3 评论 0原文

这是来自webservice的json格式:{“result”:[{“id”:“1”,“category”:“Rumah sakit”,“image”:“hospital.png”}]}

这是android代码:

public class ARPublicFacilitiesActivity extends Activity {

public static int PILIHAN = 0;

private ProgressDialog waitDialog;
private LinearLayout layout;

private String JsonKategori = "";

private ImageLoader imgLoader;

public static String image[];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    setContentView(R.layout.menudinamis);

    layout = (LinearLayout) findViewById(R.id.layout);

    waitDialog = ProgressDialog.show(ARPublicFacilitiesActivity.this, "",
            "Download category ...");
    waitDialog.setIcon(R.drawable.iconarpf);
    waitDialog.show();
    new UnduhCategoryTask().execute();

}

class UnduhCategoryTask extends AsyncTask<String, Void, Void> {

    protected Void doInBackground(String... arg0) {
        JsonKategori = HTTPConnection.openUrl(HTTPConnection.host
                + "category.php");
        System.out.println("Data : " + JsonKategori);
        return null;
    }

    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        waitDialog.dismiss();
        initButton();
    }
}

public void initButton() {

    imgLoader = new ImageLoader(this);

    // memparsing json
    try {
        JSONObject jo = new JSONObject(JsonKategori);
        JSONArray ja = jo.getJSONArray("result");
        int id = 0;
        image = new String[ja.length()];
        LinearLayout ly[] = new LinearLayout[2];
        // DrawableManager dm = new DrawableManager();
        for (int x = 0; x < ly.length; x++) {
            ly[x] = new LinearLayout(this);
            ly[x].setOrientation(LinearLayout.VERTICAL);
            ly[x].setPadding(5, 5, 5, 5);
            ly[x].setGravity(Gravity.CENTER_VERTICAL);
        }
        for (int x = 0; x < ja.length(); x++) {
            JSONObject joj = ja.getJSONObject(x);

            image[x] = joj.getString("image");
            ImageView img = new ImageView(this);
            img.setPadding(5, 5, 5, 2);
            img.setMinimumHeight(96);
            img.setMinimumWidth(96);
            img.setMaxHeight(96);
            img.setMaxWidth(96);

            final String nilai = joj.getString("id") + "";

            img.setTag(HTTPConnection.urlPicture 
                    + joj.getString("image"));
            imgLoader.DisplayImage(HTTPConnection.urlPicture 
                    + joj.getString("image"),this, img);
            img.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    PILIHAN = Integer.parseInt(nilai);
                    startActivity(new Intent(
                            ARPublicFacilitiesActivity.this, MixView.class));
                }
            });


            ly[id].addView(img);
            TextView txt = new TextView(this);
            txt.setText(joj.getString("category"));
            txt.setTextColor(Color.GRAY);
            txt.setGravity(Gravity.CENTER_HORIZONTAL);
            ly[id].addView(txt);
            if (ja.length() % 2 == 0) {
                if (x == ((ja.length() / 2) - 1)) {
                    layout.addView(ly[id]);
                    ++id;
                }
            } else {
                if (x == (ja.length() / 2)) {
                    layout.addView(ly[id]);
                    ++id;
                }
            }
        }
        layout.addView(ly[id]);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

当我运行上面的脚本时,类别标题和图像成功出现。问题是图像不是我来自网络服务的图像,而是 Android 默认图标。我一直尝试在网络服务中调整图像大小,但仍然无法正常工作。

请帮我解决它。

非常感谢。

this is the json format from webservice : {"result" : [{"id":"1","category":"Rumah sakit","image":"hospital.png"}]}

here's the android code:

public class ARPublicFacilitiesActivity extends Activity {

public static int PILIHAN = 0;

private ProgressDialog waitDialog;
private LinearLayout layout;

private String JsonKategori = "";

private ImageLoader imgLoader;

public static String image[];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    setContentView(R.layout.menudinamis);

    layout = (LinearLayout) findViewById(R.id.layout);

    waitDialog = ProgressDialog.show(ARPublicFacilitiesActivity.this, "",
            "Download category ...");
    waitDialog.setIcon(R.drawable.iconarpf);
    waitDialog.show();
    new UnduhCategoryTask().execute();

}

class UnduhCategoryTask extends AsyncTask<String, Void, Void> {

    protected Void doInBackground(String... arg0) {
        JsonKategori = HTTPConnection.openUrl(HTTPConnection.host
                + "category.php");
        System.out.println("Data : " + JsonKategori);
        return null;
    }

    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        waitDialog.dismiss();
        initButton();
    }
}

public void initButton() {

    imgLoader = new ImageLoader(this);

    // memparsing json
    try {
        JSONObject jo = new JSONObject(JsonKategori);
        JSONArray ja = jo.getJSONArray("result");
        int id = 0;
        image = new String[ja.length()];
        LinearLayout ly[] = new LinearLayout[2];
        // DrawableManager dm = new DrawableManager();
        for (int x = 0; x < ly.length; x++) {
            ly[x] = new LinearLayout(this);
            ly[x].setOrientation(LinearLayout.VERTICAL);
            ly[x].setPadding(5, 5, 5, 5);
            ly[x].setGravity(Gravity.CENTER_VERTICAL);
        }
        for (int x = 0; x < ja.length(); x++) {
            JSONObject joj = ja.getJSONObject(x);

            image[x] = joj.getString("image");
            ImageView img = new ImageView(this);
            img.setPadding(5, 5, 5, 2);
            img.setMinimumHeight(96);
            img.setMinimumWidth(96);
            img.setMaxHeight(96);
            img.setMaxWidth(96);

            final String nilai = joj.getString("id") + "";

            img.setTag(HTTPConnection.urlPicture 
                    + joj.getString("image"));
            imgLoader.DisplayImage(HTTPConnection.urlPicture 
                    + joj.getString("image"),this, img);
            img.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    PILIHAN = Integer.parseInt(nilai);
                    startActivity(new Intent(
                            ARPublicFacilitiesActivity.this, MixView.class));
                }
            });


            ly[id].addView(img);
            TextView txt = new TextView(this);
            txt.setText(joj.getString("category"));
            txt.setTextColor(Color.GRAY);
            txt.setGravity(Gravity.CENTER_HORIZONTAL);
            ly[id].addView(txt);
            if (ja.length() % 2 == 0) {
                if (x == ((ja.length() / 2) - 1)) {
                    layout.addView(ly[id]);
                    ++id;
                }
            } else {
                if (x == (ja.length() / 2)) {
                    layout.addView(ly[id]);
                    ++id;
                }
            }
        }
        layout.addView(ly[id]);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

when i run this script above, the category title and image is success appear. The problem is image that appears not my image from web service, but android default icon. I have been try to resize my image in web service but still not working.

please help me to solve it.

Thanks so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や莫失莫忘 2024-12-31 01:42:10

因为我找不到任何在 ImageView 上设置图像的代码行。
喜欢
img.setImage(YOUR_IMAGE);

Because I could not find any line of code in which you are setting image on your ImageView.
Like
img.setImage(YOUR_IMAGE);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文