是否必须设置HttpUrlConnection才能使用公共wifi连接?

时间:2014-05-17 18:25:24

标签: android http httpurlconnection

我目前正在处理的应用程序处于第一个Beta阶段,发现该类在使用公共wifi时无法完成其任务。

以前有人有这个问题吗?是否与它尝试连接的服务器使用的SSL证书有关?

public class NetGameList implements Callable<String> {

    private DatabaseManager DBM = DatabaseManager.getInstance();

    private JSONArray jsonArray;

    @Override
    public String call() throws Exception {

        HttpURLConnection connection;
        JSONObject jsonParam = new JSONObject();

        try {

            URL url = new URL(Constants.NETBUILDGAMELISTURL);
            connection = (HttpURLConnection) url.openConnection();

            jsonParam.put("TAG", Constants.HTTPTHREE);
            jsonParam.put("receiverid", Constants.NETUSERFACEBOOKID);

            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            //write data to server-------------------------------------------------------------------------
            DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());

            outputStream.write(jsonParam.toString().getBytes("utf-8"));

            //flush&close the stream
            outputStream.flush();
            outputStream.close();


            //Get result from server
            InputStream responseStream = new BufferedInputStream(connection.getInputStream());

            BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream));
            String line = "";

            ArrayList<String> jsonArrays = new ArrayList<String>();

            while ((line = responseStreamReader.readLine()) != null) {

                jsonArrays.add(line);
            }
            responseStreamReader.close();

            for (String game : jsonArrays) {

                JSONObject jsonObject = new JSONObject(game);

                jsonArray = jsonObject.getJSONArray("data");

                for (int i = 0; i < jsonObject.length(); i++) {

                    JSONObject jObject = jsonArray.getJSONObject(i);


                    if (!DBM.gameExists(jObject.get("GameId").toString())) {

                        DBM.addGame(jObject.get("GameId").toString(), jObject.get("SenderId").toString(),
                                jObject.get("GameTrack").toString(), jObject.get("FileLocation").toString(), getDateTime());
                    } else {

                        DBM.updateGames(jObject.get("GameId").toString(), jObject.get("SenderId").toString(),
                                jObject.get("GameTrack").toString(), jObject.get("FileLocation").toString(), getDateTime());
                    }
                }
            }

            responseStream.close();
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return Constants.NETOPCOMPLETE;
    }

    private String getDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "dd/MM/yyyy", Locale.getDefault());
        Date date = new Date();
        return dateFormat.format(date);
    }
}

2 个答案:

答案 0 :(得分:0)

要使用WIFI,您应该添加权限:

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

到您的清单文件。

答案 1 :(得分:0)

有时,&#34;公众&#34; wifi需要基于网络的登录