问题
我正在尝试从网站解析XML文件。我正在使用HTTP连接从网站获取XML内容。在我下载内容时,我遇到了一些问题。代码如下。
CODE
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.button);
}
public void process(View view) {
Thread thread=new Thread(new Download());
thread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class Download implements Runnable {
public void run() {
try {
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.getDoOutput();
InputStream stream = connection.getInputStream();
Toast.makeText(MainActivity.this, stream + "", Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
这是我用来下载内容的代码。我得到的错误是
错误
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "api.androidhive.info": No address associated with hostname
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at com.example.sajithm.domparsing.MainActivity$Download.run(MainActivity.java:65)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
10-21 01:14:21.459 25125-25181/com.example.sajithm.domparsing W/System.err﹕ ... 15 more
--------- beginning of /dev/log/system
10-21 01:19:34.259 25125-25131/com.example.sajithm.domparsing D/dalvikvm﹕ GC_FOR_ALLOC freed 306K, 4% free 9003K/9336K, paused 5ms, total 5ms
请求 网址有效。任何人都可以建议解决这个问题的解决方案。有没有其他方式下载内容?
答案 0 :(得分:0)
我尝试了下面的代码并且有效:
拨打电话的方法:
public void makeXMLRequest() {
try {
URL url = new URL("http://api.androidhive.info/pizza/?format=xml");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.getDoOutput();
String readStream = readStream(con.getInputStream());
// Give output for the command line
System.out.println(readStream);
} catch (Exception e) {
e.printStackTrace();
}
}
解析响应的参考方法:
private static String readStream(InputStream in) {
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in));) {
String nextLine = "";
while ((nextLine = reader.readLine()) != null) {
sb.append(nextLine);
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
如何调用此方法:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
makeXMLRequest();
}
});
thread.start();
如果有帮助,请尝试此操作。
谢谢..!
答案 1 :(得分:0)
虽然您可以通过计算机的网络浏览器访问该网站。我也可以访问该链接。 但这并不意味着您的Android手机可以解析域名。 请使用adb shell尝试
ping api.androidhive.info
您很可能会看到否定回复。
答案 2 :(得分:0)
这是模拟器的问题。在某些情况下,虽然可以通过我们的PC浏览器访问网站,但模拟器无法访问网站。我在Genymotion中重新安装了虚拟设备(在我的情况下是Nexus 4.3),现在一切正常,模拟器能够访问所有网站。
谢谢