0

我想在我的 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();
    }

}

应用程序 android 清单包含两个权限:

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

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

4

0 回答 0