Android Dev: How do I extract data from the web and use the data in my app?

后端 未结 2 1383
暗喜
暗喜 2020-12-14 05:16

I saw this android app called Lyrics App. It provides the lyrics of the song currently played but needs internet connection always. Now, I wanted to make a similar app, that

相关标签:
2条回答
  • 2020-12-14 05:31

    Try...

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://www.someplace.com");
    ResponseHandler<String> resHandler = new BasicResponseHandler();
    String page = httpClient.execute(httpGet, resHandler);
    

    This can be used to grab the whole webpage as a string of html, i.e., "<html>...</html>"

    EDIT: Note, you need to declare the following 'uses-permission' in the android manifest xml file...

    <uses-permission android:name="android.permission.INTERNET" />
    
    0 讨论(0)
  • 2020-12-14 05:51

    To those that don't already know, each of your android programs has a file called a manifest.

    The manifest content is xml, and typically looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    
    <manifest>
    
        <uses-permission />
        <permission />
        <permission-tree />
        <permission-group />
        <instrumentation />
        <uses-sdk />
        <uses-configuration />  
        <uses-feature />  
        <supports-screens />  
        <compatible-screens />  
        <supports-gl-texture />  
    
        <application>
    
            <activity>
                <intent-filter>
                    <action />
                    <category />
                    <data />
                </intent-filter>
                <meta-data />
            </activity>
    
            <activity-alias>
                <intent-filter> . . . </intent-filter>
                <meta-data />
            </activity-alias>
    
            <service>
                <intent-filter> . . . </intent-filter>
                <meta-data/>
            </service>
    
            <receiver>
                <intent-filter> . . . </intent-filter>
                <meta-data />
            </receiver>
    
            <provider>
                <grant-uri-permission />
                <meta-data />
                <path-permission />
            </provider>
    
            <uses-library />
    
        </application>
    
    </manifest>
    

    Along with other things, this file is where you specify the uses permissions that your program uses e.g. if it needs to make a phone-call, to upload data etc.

    These uses permissions invoke a question-box each upon application installation, giving the user the opportunity to allow or deny your application various services.

    Bear (Rawr!) this in mind when programming, because your application will be a better if a user denies your application from uploading and your application still works.

    Specific to your application, you need the internet permission. To enable it, add the following uses permission in the manifest file wherever you wish within the tag at the top and the tag at the foot:

    <uses-permission android:name="android.permission.INTERNET" />
    

    This will prompt the user to decide, "Do you want to allow this application internet access?"

    0 讨论(0)
提交回复
热议问题