私の Android アプリは、SSL および GET 要求を使用して Web サーバーと通信しています。API 10 (Gingerbread) に切り替えた後、SSL 接続は機能しますが、アプリの起動後初めて...
最初のリクエストはメイン アクティビティによって送信されます。応答を取得した後、別のアクティビティが開始され、複数のリクエストが送信されます。そして、それらのどれも答えられません。どちらの場合も、リクエストは、新しい AsyncTask で開始される小さな WebService クラスを使用して送信されます。この残念なことにサイズを縮小した後、実際に含まれているのは URL(-String) だけです。各アクティビティは、このクラスの独自のインスタンスを開始します。
GET リクエストを実行するメソッドは次のとおりです。簡単に見えるように、キープアライブを回避するためのコードをいくつか含めました-嫌いではありませんが、複数の接続の問題を回避するために、他の回答でそうすることが提案されています。まあ、私の場合はうまくいきませんでした。
public String webGet(String methodName, Map<String, String> params) {
String getUrl = webServiceUrl + methodName;
index++;
final int connectionID = index;
int i = 0;
for (Map.Entry<String, String> param : params.entrySet()) {
if (i == 0) {
getUrl += "?";
} else {
getUrl += "&";
}
try {
getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
String response;
Log.e("WebGetURL", "["+connectionID+"] " + getUrl);
URL url;
try {
url = new URL(getUrl);
} catch (MalformedURLException e) {
Log.e("WebService", "Malformed URL: " + getUrl);
return null;
}
HttpURLConnection urlConnection;
try {
Log.e("WebGetResponse", "["+connectionID+"] openConnection()");
System.setProperty("http.keepAlive", "false");
if (webServiceSsl) {
Log.e("WebService", "Using HTTPS");
urlConnection = (HttpsURLConnection) url.openConnection();
} else {
urlConnection = (HttpURLConnection) url.openConnection();
}
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Connection","Keep-Alive");
urlConnection.setDoOutput(false);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("GET");
} catch (IOException e) {
Log.e("WebService", "I/O exception opening connection: " + e.getMessage());
e.printStackTrace();
return null;
}
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(10000);
urlConnection.setRequestProperty("Connection", "close");
try {
urlConnection.connect();
Log.e("WebGetResponse", "["+connectionID+"] getInputStream()");
// This is the last thing I hear from my thread
BufferedInputStream bin = new BufferedInputStream(urlConnection.getInputStream());
Log.e("WebGetResponse", "["+connectionID+"] gotInputStream()");
byte[] contents = new byte[1024];
int bytesRead=0;
StringBuilder strFileContents = new StringBuilder();
Log.e("WebGetResponse", "["+connectionID+"] Waiting for data");
while((bytesRead = bin.read(contents)) != -1) {
String add = new String(contents, 0, bytesRead);
strFileContents.append(add);
}
bin.close();
response = strFileContents.toString();
} catch (IOException e) {
Log.e("WebService", "I/O exception reading stream: " + e.getMessage());
e.printStackTrace();
return null;
} finally {
urlConnection.disconnect();
}
Log.e("WebGetResponse", "["+connectionID+"] " + response);
return response;
}
私は検索を試みてきました - 私は問題を理解していません。実際、現在、https 以外のサーバーでクラスをテストすることはできないため、HTTP でも問題が発生するかどうかはわかりません。ただし、最初の要求がうまく機能するため、ハンドシェイクは機能しているように見えます。
リクエストを開始するコードは次のとおりです (最後のパラメーターは、送信する GET コンテンツです)。
class ServerDataThread extends AsyncTask<Integer, Integer, String[]> {
@Override
protected String[] doInBackground(Integer... attempts) {
sendActive++;
int count = attempts.length;
String[] responses = new String[count];
for (int i = 0; i < count; i++) {
responses[i] = server.webGet("collector.php", params);
}
return responses;
}
protected void onPostExecute(String[] responses) {
sendActive--;
for (int i = 0; i < responses.length; i++) {
if (responses[i] == null) {
continue;
}
onResponseData(responses[i]);
}
}
}
new ServerDataThread().execute(0);
私が間違っていることのヒントを教えてください。どうもありがとうございました!
バーニンレオ