试图从Android活动访问网址

时间:2012-09-09 10:53:28

标签: android httpurlconnection

我想在我的android活动中访问一个url,用于存储一个字符串,该字符串在url本身作为参数传递,并带有我已构建的web服务。在这一点上,我只想访问该URL并看到它发生,稍后我会担心对结果做一些事情(请求返回的内容)。我正在使用HttpURLConnection。我收到了这个错误:

D/SntpClient(   59): request time failed: java.net.SocketException: Address fami
ly not supported by protocol

我的代码是:

public void storeScore()
{
    Log.d("storeScore","is running");
    HttpURLConnection con = null;
    //DefaultHttpClient
    try {
        URL url = new URL("http://www.google.com");
        con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(10000);
        con.setConnectTimeout(15000);
        con.setRequestMethod("GET");
        con.setDoInput(true); //also tried without this
        con.setDoOutput(true); //also tried without this
        con.connect();
        Log.d("storeScore","got to connect");
        //not really reading the result at this point:
        //BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        //String payload = reader.readLine();
        //reader.close();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d("MalformedURLException",e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.d("IOException",e.getMessage());
        e.printStackTrace();
    }

}

app android清单包括两个提交:

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

此方法storeScore()从在创建时创建并启动的线程运行。我在这里做错了吗?如果您需要我在此处未添加的更多相关信息,请告诉我,我将编辑该问题。谢谢,我感谢任何协助。

0 个答案:

没有答案