使用适用于Android的loopj库调用 GET 或 POST 时,我似乎遇到了间歇性的SocketTimeout异常。
但是,此错误似乎仅在某些版本的 android 上是一个问题。话虽如此,我已经删除了我在 api 类上的 SSL 证书设置,现在我只是调用标准的 http get/post。
间歇性抛出的错误:
java.net.SocketTimeoutException:读取超时
或者
javax.net.ssl.SSLException: Write error: ssl=0x6118a8: I/O error during system call, Broken pipe
我检查了 apache 上的错误日志,我得到:
客户端过早关闭连接,因此在向上游发送请求时也关闭了上游连接
我不确定这是否是一个问题?
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import android.util.Log;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
public class Api {
private static String api_url = "http://myurl.co.uk/api";
private static VenueManager venueManager;
private static final String BASE_URL = "http://myurl.co.uk/api";
private static boolean success = false;
private static AsyncHttpClient client = null;
public static final String api_key = "API_KEY";
public static final String api_secret = "API_SECRET";
static final int DEFAULT_TIMEOUT = 20 * 1000;
// public static AsyncHttpClient get_client()
// {
// if(client != null)
// {
// return client;
// }
// else
// {
// client = new AsyncHttpClient();
// try
// {
// KeyStore trustStore = //KeyStore.getInstance(KeyStore.getDefaultType());
// trustStore.load(null, null);
// SocketFactory sf = new SocketFactory(trustStore);
// //sf.setHostnameVerifier(SocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
// client.setSSLSocketFactory(sf);
// client.setTimeout(DEFAULT_TIMEOUT);
// }
// catch (Exception e)
// {
// Log.v("API", e.toString());
// }
//
// return client;
// }
// }
public static String get_api_url()
{
return api_url;
}
public static String search_venues(double lat, double lng)
{
return get_api_url() + "/venues?lat=" + lat + "&lng=" + lng + "&radius=1.0";
}
public static String post_review()
{
return get_api_url() + "/review";
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(5, DEFAULT_TIMEOUT);
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(5, DEFAULT_TIMEOUT);
client.post(getAbsoluteUrl(url), params, responseHandler);
}
public static String sha1(String s) {
MessageDigest digest = null;
try
{
digest = MessageDigest.getInstance("SHA-1");
}
catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
digest.reset();
byte[] data = digest.digest(s.getBytes());
return String.format("%0" + (data.length*2) + "X", new BigInteger(1, data));
}
}
以上是我的 API 类。以下是我在活动中调用的方法
public void check_survey()
{
RequestParams params = new RequestParams();
params.put("email", sharedPreferences.getString("email", null));
params.put("password", sharedPreferences.getString("password", null));
params.put("api_key", Api.api_key);
Api.get("/survey", params, new AsyncHttpResponseHandler() {
@Override
public void onFailure(Throwable e) {
Toast.makeText(HomeActivity.this, e.toString(), Toast.LENGTH_LONG).show();
loading.dismiss();
}
@Override
public void onFailure(Throwable e, String response) {
loading.dismiss();
}
@Override
public void onFinish() {
loading.dismiss();
}
@Override
public void onSuccess(String response) {
try
{
JSONObject result = new JSONObject(response);
JSONObject meta = result.getJSONObject("meta");
if(meta.getBoolean("success"))
{
if(result.getString("response").equals("true"))
{
// do something
}
}
else
{
// display errors
}
}
catch(Exception e)
{
Log.v("Error", e.toString());
}
loading.dismiss();
}
});
}
对于为什么会发生这种情况,我真的很困惑。