我目前正在使用的應用程序是在它的第一個Beta階段,發現這個類在使用公共WiFi時無法完成它的任務。HttpUrlConnection是否必須設置爲使用公共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);
}
}
我刪除了您的Java標記。這個問題純粹是關於Android,它與Java無關。如果沒有Android經驗,請不要將Java標籤添加到Java開發人員無法回答的問題中。 – drewmoore
@drewmore http://meta.stackoverflow.com/questions/254642/deleting-java-tag-from-questions-tagged-androidjava – laalto
請刪除代碼示例中的垃圾,並保留允許重現該問題的部分。另外,正如我在我的回答中所評論的那樣,不要吞下異常,什麼是日誌,當jsonArrays沒有完成時,它的價值是什麼? – rds