簡體   English   中英

android-無法從網站解析xml

[英]android - cannot parse xml from website

我試圖在我的網站“ 這里”解析一個xml文件,但是當我打開應用程序時,它沒有顯示任何內容,我認為我的布局還可以,這是我的xml問題嗎? 我在問題中讀到它說更好地使用json和asynctask,但我也嘗試過,它不起作用,所以我更改為xml,但仍然無法正常工作。
這是我的代碼

package org.redeagle.growtopiamarket;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout mainLayout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        mainLayout.setLayoutParams(layoutParams);
        mainLayout.setPadding(12,12,12,12);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        try {
            URL urls = new URL("https://growtopiajson.000webhostapp.com/gtpost.xml");
            URLConnection conn = urls.openConnection();
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(conn.getInputStream());
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("post");

            System.out.println("----------------------------");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;
                    String strImageURL = eElement.getElementsByTagName("imageurl").item(0).getTextContent();
                    String strItemName = eElement.getElementsByTagName("itemname").item(0).getTextContent();
                    String strItemDesc = eElement.getElementsByTagName("itemdesc").item(0).getTextContent();
                    String strItemPrice = eElement.getElementsByTagName("itemprice").item(0).getTextContent();
                    String strItemSeller = eElement.getElementsByTagName("itemseller").item(0).getTextContent();
                    RelativeLayout postLayout = new RelativeLayout(this);
                    RelativeLayout.LayoutParams postParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    postParams.setMargins(0, 0, 0, 7);
                    postLayout.setLayoutParams(layoutParams);
                    postLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.shadowlinear));

                    ImageView imageView = new ImageView(this);
                    RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    imageView.setLayoutParams(imageParams);
                    imageView.setId(Integer.parseInt("itemImage"));
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    URL url = new URL(strImageURL);
                    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    imageView.setImageBitmap(bmp);

                    TextView itemName = new TextView(this);
                    RelativeLayout.LayoutParams itemNameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    itemNameParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    itemNameParams.addRule(RelativeLayout.ALIGN_PARENT_END);
                    itemNameParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    itemNameParams.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
                    itemNameParams.addRule(RelativeLayout.END_OF, imageView.getId());
                    itemName.setId(Integer.parseInt("itemName"));
                    itemName.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
                    itemName.setTextSize(27);
                    itemName.setTypeface(null, Typeface.BOLD);
                    itemName.setText(strItemName);
                    itemName.setLayoutParams(itemNameParams);

                    TextView itemDesc = new TextView(this);
                    RelativeLayout.LayoutParams itemDescParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    itemDescParams.addRule(RelativeLayout.BELOW, imageView.getId());
                    itemDescParams.addRule(RelativeLayout.ALIGN_PARENT_START);
                    itemDescParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    itemDesc.setTextSize(12);
                    itemDesc.setId(Integer.parseInt("itemDesc"));
                    itemDesc.setText(strItemDesc);
                    itemDesc.setLayoutParams(itemDescParams);

                    TextView itemPrice = new TextView(this);
                    RelativeLayout.LayoutParams itemPriceParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    itemPrice.setId(Integer.parseInt("itemPrice"));
                    itemPriceParams.addRule(RelativeLayout.BELOW, itemDesc.getId());
                    itemPriceParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    itemPriceParams.addRule(RelativeLayout.ALIGN_PARENT_START);
                    itemPrice.setTextSize(13);
                    itemPrice.setText(strItemPrice);
                    itemPrice.setLayoutParams(itemPriceParams);

                    TextView itemSeller = new TextView(this);
                    RelativeLayout.LayoutParams itemSellerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                    itemSellerParams.addRule(RelativeLayout.ALIGN_TOP, itemPrice.getId());
                    itemSellerParams.addRule(RelativeLayout.RIGHT_OF, itemPrice.getId());
                    itemSellerParams.setMarginStart(9);
                    itemSellerParams.setMargins(9, 0, 0, 0);
                    itemSellerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                    itemSellerParams.addRule(RelativeLayout.ALIGN_PARENT_END);
                    itemSeller.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END);
                    itemSeller.setId(Integer.parseInt("itemSeller"));
                    itemSeller.setTextSize(13);
                    itemSeller.setText(strItemSeller);
                    itemSeller.setLayoutParams(itemSellerParams);

                    postLayout.addView(imageView);
                    postLayout.addView(itemName);
                    postLayout.addView(itemDesc);
                    postLayout.addView(itemPrice);
                    postLayout.addView(itemSeller);

                    mainLayout.addView(postLayout);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        setContentView(mainLayout);
    }
    private static Document loadTestDocument(String url) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(new URL(url).openStream());
    }
}

無論如何,感謝您的回答,即時消息對編程還是很新鮮的。

您的問題可能是不允許在主線程上調用與網絡相關的代碼,如此處所述

我試圖運行您的實現,並得到了NetworkOnMainThreadException

根據此答案,這是在AsyncTask運行這些代碼的好模式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM