开发者

UnknownHostException (webservice http-get)

开发者 https://www.devze.com 2023-04-08 19:11 出处:网络
I have this simple code, that makes a get to a webservice. For some reason it cant connect and gives the error in the log unknownHostException

I have this simple code, that makes a get to a webservice. For some reason it cant connect and gives the error in the log unknownHostException

This is the code:

String URL = "http://services.sapo.pt/EPG/GetChannelList";
String result = "";
final String tag = "Data received: ";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button btnSearch = (Button)find开发者_开发技巧ViewById(R.id.btnSearch);
    btnSearch.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            callWebService();
        }
    });

} // end onCreate()

public void callWebService(){
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);
    ResponseHandler<String> handler = new BasicResponseHandler();

    try {
        result = httpclient.execute(request, handler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    httpclient.getConnectionManager().shutdown();
    Log.i(tag, result);
}

This is part of my manifest

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".AndroidApp"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


I found the solution. I putted the internet permission using the GUI in manifest which added the tag

< permission android:name"android.permission.INTERNET"/>

Then by hand, i putted the correct tag that is:

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

which is a different tag and it worked. GUI fail.


Not sure if you have already checked this, but please check if you are behind a proxy or firewall. Here is a way to configure the HttpClient for a proxy and official apache documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消