我需要jni调用android的http get请求,每次都返回IOException: The operation timed out。
但是在浏览器中能够访问请求的url。请问:我怎么才能在jni调用中成功访问呢?
请求代码如下:
//创建httpRequest对象
HttpGet httpRequest = new HttpGet(url);
try
{
//取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
//请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//取得返回的字符串
……
}
else
{
Log.e("请求错误!","请求错误!");
}
}
catch (IOException e)
{
Log.e("IOException",e.getMessage().toString());
}
但是在浏览器中能够访问请求的url。请问:我怎么才能在jni调用中成功访问呢?
请求代码如下:
//创建httpRequest对象
HttpGet httpRequest = new HttpGet(url);
try
{
//取得HttpClient对象
HttpClient httpclient = new DefaultHttpClient();
//请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
//请求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//取得返回的字符串
……
}
else
{
Log.e("请求错误!","请求错误!");
}
}
catch (IOException e)
{
Log.e("IOException",e.getMessage().toString());
}
urlCon.setConnectTimeout(30*1000);
urlCon.setReadTimeout(30*1000);
HttpGet hp = new HttpGet(url);
httpClient = new DefaultHttpClient();
String netType = isNetType(context);
if (netType != null & netType.equals("cmwap")) {
HttpHost proxy = new HttpHost("10.0.0.172", 80);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY,
proxy);
}
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),
5 * 1000);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), 60 * 1000);
HttpResponse response = httpClient.execute(hp);
if (response.getStatusLine().getStatusCode() == 200) {
inputstream = response.getEntity().getContent();
/**
* 判断接入点类型
*
* @return
*/
public static String isNetType(Context context) {
String nettype = null;
if (context == null) {
return null;
}
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobNetInfo = connectivityManager.getActiveNetworkInfo();
if (mobNetInfo != null) {
if (mobNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
nettype = mobNetInfo.getTypeName(); // 当前联网类型是WIFI
} else {
nettype = mobNetInfo.getExtraInfo();// 当前联网类型是cmnet/cmwap
}
} return nettype;
}