所以我正在尝试加载一个webpage看起来像我需要将它保存到我的Android设备上的xml文档中:P
我看过这篇文章How to save data from a link in a xml file in android?并复制了答案,但我收到了这个错误
logcat代码
11-19 20:59:57.219: W/System.err(18965): android.os.NetworkOnMainThreadException
11-19 20:59:57.219: W/System.err(18965): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1208)
11-19 20:59:57.219: W/System.err(18965): at java.net.InetAddress.lookupHostByName(InetAddress.java:388)
11-19 20:59:57.219: W/System.err(18965): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
11-19 20:59:57.219: W/System.err(18965): at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-19 20:59:57.219: W/System.err(18965): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-19 20:59:57.219: W/System.err(18965): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-19 20:59:57.219: W/System.err(18965): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-19 20:59:57.219: W/System.err(18965): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367)
11-19 20:59:57.219: W/System.err(18965): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:592)
11-19 20:59:57.229: W/System.err(18965): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:510)
11-19 20:59:57.229: W/System.err(18965): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:488)
11-19 20:59:57.229: W/System.err(18965): at com.example.eve_api_app.MainActivity.executeHttpGet(MainActivity.java:70)
11-19 20:59:57.229: W/System.err(18965): at com.example.eve_api_app.MainActivity$1.onClick(MainActivity.java:45)
11-19 20:59:57.229: W/System.err(18965): at android.view.View.performClick(View.java:4102)
11-19 20:59:57.229: W/System.err(18965): at android.view.View$PerformClick.run(View.java:17085)
11-19 20:59:57.229: W/System.err(18965): at android.os.Handler.handleCallback(Handler.java:615)
11-19 20:59:57.239: W/System.err(18965): at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 20:59:57.239: W/System.err(18965): at android.os.Looper.loop(Looper.java:155)
11-19 20:59:57.239: W/System.err(18965): at android.app.ActivityThread.main(ActivityThread.java:5454)
11-19 20:59:57.239: W/System.err(18965): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 20:59:57.239: W/System.err(18965): at java.lang.reflect.Method.invoke(Method.java:511)
11-19 20:59:57.239: W/System.err(18965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
11-19 20:59:57.239: W/System.err(18965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
11-19 20:59:57.239: W/System.err(18965): at dalvik.system.NativeStart.main(Native Method)
这是我的应用代码
package com.example.eve_api_app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView name,corp;
Button load;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView)findViewById(R.id.textView1);
corp = (TextView)findViewById(R.id.textView3);
load = (Button) findViewById(R.id.button1);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
try {
executeHttpGet();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
// display error
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void executeHttpGet() throws Exception {
corp.setText("rim");
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("https://api.eveonline.com/account/characters.xml.aspx?keyID=2795171&vCode=T3VQGPHimVmpgmmqx0fDtegK2sIxjDuZxH5MevPhZQihYlynZG3HmL2aXo3KsTGz"));
HttpResponse response = client.execute(request);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String page = sb.toString();
System.out.println(page);
corp.setText(page);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我在控制台中有托盘som cod纯java代码看起来像这样
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import javax.sql.rowset.spi.XmlReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
public static void main(String[] args) {
download();
read();
}
private static void read() {
// TODO Auto-generated method stub
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dbuilder;
try {
dbuilder = builderFactory.newDocumentBuilder();
Document document = dbuilder.parse(XmlReader.class.getResourceAsStream("/api.xml"));
document.normalize();
NodeList rootNodes = document.getElementsByTagName("eveapi");
Node RootNode = rootNodes.item(0);
Element rooteElement = (Element) RootNode;
NodeList resultNote = rooteElement.getElementsByTagName("result");
Node resultnode = resultNote.item(0);
Element resultElement = (Element) resultnode;
NodeList rowsetList = resultElement.getElementsByTagName("rowset");
Node rowsetNode = rowsetList.item(0);
Element rowserElement = (Element) rowsetNode;
NodeList rowset = rowserElement.getElementsByTagName("row");
for (int i = 0; i < rowset.getLength(); i++) {
Node theNote = rowset.item(i);
Element noteElement = (Element) theNote;
System.out.println(noteElement.getAttribute("name"));
System.out.println(noteElement.getAttribute("corporationName"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void download() {
// TODO Auto-generated method stub
try {
URL oracle = new URL(
"https://api.eveonline.com/account/characters.xml.aspx?keyID=2795171&vCode=T3VQGPHimVmpgmmqx0fDtegK2sIxjDuZxH5MevPhZQihYlynZG3HmL2aXo3KsTGz");
BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
String inputLine;
Writer writer = null;
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("res/api.xml"), "utf-8"));
while ((inputLine = in.readLine()) != null){
writer.write(inputLine);
}
writer.close();
} catch (Exception ex) {
// report
}
}
}
哪个有效,但是当我将它改编为android时它不会工作:(
任何人都知道如何做到这一点并将其保存到SD卡?
答案 0 :(得分:0)