当使用 loopj 库为 Android 调用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();
}
});
}
为什么会发生这种情况,我真的处于松散状态。