我已经处理了很长时间的代码,我无法弄明白,那里有什么问题。
我有一个简单的课程TwitterActivity
。在里面我还有一个mSignin
按钮。我为mSignin
按钮设置了事件处理,在onClick(..)
方法中,我调用了一个名为CheckInternetConnection
的内部类,它扩展了AsyncTasck
。
然后在doInBackground(...)
方法中,我尝试通过打开与"http://www.google.com"
的连接来获取响应代码。
结果我总是得到IOexception。我尝试了不同的ConnectTimeout
和不同的网站,但没有任何改变。
这里可能出现什么问题?感谢。
public class TwitterActivity extends Activity
{
private static final String LOG_TAG = null;
private Button mSignin;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter);
mSignin = (Button)findViewById(R.id.login_id);
mSignin.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new CheckInternetConnection().execute();
}
});
}
class CheckInternetConnection extends AsyncTask<Void, Void, Boolean>{
@Override
protected void onPostExecute(Boolean result){
if(result)
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "failure", Toast.LENGTH_SHORT).show();
}
@Override
protected Boolean doInBackground(Void... params) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL( "http://www.google.com" ).openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Log.e(LOG_TAG, "Error checking internet connection", e);
}
} else {
Log.d(LOG_TAG, "No network available!");
}
return false;
}
}
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}
编辑1:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bledi.android.twittertest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="TwitterActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".SearchTwitter"
android:label="@string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="oauth" android:host="com.bledi.TwitterTest" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
</application>
</manifest>
编辑2
如果有人遇到同样的问题,请勿忘记在真实设备中尝试。模拟器有时很糟糕,你不知道为什么。
答案 0 :(得分:0)
如果您只想查看是否有互联网连接,可以使用isNetworkAvailable()
和activeNetworkInfo.isConnected()
。这足以验证互联网的可用性。
public boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
如果你真的想ping http://www.google.com,请试试这个:
protected Boolean doInBackground(Void... params) {
if (isNetworkAvailable()) {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL( "http://www.google.com" ).openConnection());
urlc.connect();
int resp = urlc.getResponseCode();
return resp == 200;
} catch (IOException e) {
Log.e("DD", "Error checking internet connection", e);
}
} else {
Log.d("DD", "No network available!");
}
return false;
}