我试图在我的网站Here解析一个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());
}
}
无论如何,感谢您的回答,我仍然是编程的新手。
答案 0 :(得分:0)
您的问题可能是不允许在主线程上调用与网络相关的代码,如here所示。
我尝试运行您的实施并获得NetworkOnMainThreadException
。