2

我试图从 last.fm 提出请求,我将从我输入的那个中获得类似的艺术家。我收到此错误:java.net.UnknownHostException: ws.audioscrabbler.com - 为什么?这本来是要在之后列出的,但不知何故我认为我没有“进入”他们的 api。当我按下按钮时,什么也没有发生,但在 LogCat 中,我收到了大约 25 个警告。也许reuest通过但我没有显示任何东西。当我在具有不同 pi 的不同站点上尝试此示例时,它起作用了。

// 主要的

    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);
            }

        });

    }
}
4

1 回答 1

4

I get this error: java.net.UnknownHostException

当您没有android.permission.INTERNETManifest.xml文件中给出时,会发生上述错误。

于 2012-10-07T12:53:38.247 回答