如何正确使用kso​​ap2获取歌词API

时间:2018-11-29 21:29:23

标签: android android-fragments soap android-ksoap2

我正在尝试显示在我的应用程序(音乐播放器应用程序)中播放的歌曲的歌词。

我找到了一个有关如何在youtube中实现SOAP服务的教程,但还不清楚,所以我所能做到的就是这样:

//Get Lyrics API
private class SoapCall extends AsyncTask<String, Object, String> {

    public static final String NAMESPACE = "http://api.chartlyrics.com/";
    public static final String URL = "http://api.chartlyrics.com/apiv1.asmx?WSDL";
    public static final String SOAP_ACTION = "http://api.chartlyrics.com/SearchLyricDirect";
    public static final String METHOD_NAME = "SearchLyricDirect";
    public int TimeOut = 30000;
    String response;


    @Override
    protected String doInBackground(String... strings) {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE transportSe = new HttpTransportSE(URL, TimeOut);
        transportSe.debug = true;

        try {
            transportSe.call(SOAP_ACTION, envelope);
            response = (String) envelope.getResponse();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Error", e.toString());
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (result != null) {
            testSoapTxt.setText(result);
        } else {
            Toast.makeText(getActivity(), "Something went wrong with API", Toast.LENGTH_LONG).show();
        }
    }
}

但是尝试显示一些歌词时,我得到了java.net.SocketTimeoutException。 我调试了该应用程序,发现response为空。

我正在使用此API: SOAP API to get lyrics
我已遵循本教程: SOAP API tutorial

出于测试目的,我只想看看是否可以在textView中显示任何歌词。如果我能做得到,请继续我的旅程并正确实施。

编辑:我找到了一种解决方法,可以在我的视图中手动插入歌词,但是看起来不太方便(太乱了)。

//Add songs to my list
    arrayList.add(new Song("Akon", "-Dont matter", R.raw.akon_dontmatter, R.drawable.akon, "Konvict, konvict, konvict\n" +
            "Oh, oh, oh, ooh, oh, oh\n" +
            "Oh, oh, oh, ooh, oh, oh\n" +
            "Nobody wanta' see us together\n" +
            "But it don't matter, no ('cause I got you babe)\n" +
            "Nobody wanta' see us together\n" +
            "But it don't matter, no ('cause I got you babe)\n" +
            "Cause we gonna fight, oh yes, we gonna fight (we gonna fight)\n" +
            "Believe we gonna fight (we gonna fight)\n" +
            "Fight for our right to love, yeah (right to love, yeah)\n" +
            [...]

有什么建议吗?

谢谢。

0 个答案:

没有答案