我试图从last.fm提出请求,我将从我输入的那个获得类似的艺术家。我收到此错误:java.net.UnknownHostException:ws.audioscrabbler.com - 为什么? 这是为了事后列出但不知何故我不认为我“进入”他们的api。当我按下按钮时没有任何反应,但在LogCat中我收到大约25个警告。也许reuest会通过,但我不显示任何东西。当我在具有不同pi的不同站点上尝试此示例时,它可以工作。
// MAIN
package com.example.eserviceapp;
import android.os.Bundle;
import android.app.Activity;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class Eservice_main extends Activity {
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter aa;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button enterButton = (Button) findViewById(R.id.enter);
enterButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
list = new ArrayList<String>();
EditText artistText = (EditText) findViewById(R.id.artisttext);
String artistNWS = artistText.getText().toString().replace(' ', '+');
String apiKey = "&api_key=b25b959554ed76058ac220b7b2e0a026";
URL text;
try {
text = new URL("http://ws.audioscrobbler.com/2.0/?method=artist.getSimilar&artist=cher&api_key=1e1f63e2c64fd7d8a7340cbbc3e365c3");
XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserCreator.newPullParser();
parser.setInput(text.openStream(), null);
int parserEvent = parser.getEventType();
String tagName;
int i = 0;
while (parserEvent != XmlPullParser.END_DOCUMENT) {
switch (parserEvent) {
case XmlPullParser.START_TAG:
tagName = parser.getName();
System.out.println(tagName);
if (tagName.equals("name"))
{
list.add(parser.nextText());
}
break;
}
parserEvent = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
aa = new ArrayAdapter(Eservice_main.this,
android.R.layout.simple_list_item_1, list);
ListView lw = (ListView) findViewById(R.id.listView1);
lw.setAdapter(aa);
}
});
}
}
答案 0 :(得分:4)
I get this error: java.net.UnknownHostException
如果您的android.permission.INTERNET
文件中未提供Manifest.xml
,则会出现上述错误。