无法获取响应/状态代码,始终使用 HttpUrlConnection 获取空白异常



我不知道会发生什么,但我的代码无法获得响应/状态代码,并且总是得到空白异常。

if (!GetS1.isEmpty() || !GetS1.equals("")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
url = new URL(GetS1);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(2000);
urlConnection.setReadTimeout(2000);
urlConnection.connect();
final int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
ButtonS1.setEnabled(true);
ButtonS1.setText("ACTIVE");
} else if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
ButtonS1.setText("BLOCKED");
} else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
ButtonS1.setText("DOWN");
} else {
ButtonS1.setText("UNKNOWN / BROKEN");
}
} catch (Exception e) {
Toast.makeText(Act_Details.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}

我错过了什么吗? 我已经在清单中使用互联网设置了权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

首先,请发布完整的代码,或者至少是上述代码(您提供的(所属的代码, 假设您在与 UI 线程不同的 thead 中执行整个工作,您可以执行以下操作:

if (!GetS1.isEmpty() || !GetS1.equals("")) {
doNetworkCall();
}

然后制定一个方法:

private void doNetworkCall(){
try {
url = new URL(GetS1);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(2000);
urlConnection.setReadTimeout(2000);
urlConnection.connect();
final int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
ButtonS1.setEnabled(true);
ButtonS1.setText("ACTIVE");
} 
else if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
ButtonS1.setText("BLOCKED");
}
else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
ButtonS1.setText("DOWN");
} 
else {
ButtonS1.setText("UNKNOWN / BROKEN");
}
} catch (Exception e) {
Toast.makeText(Act_Details.this, ""+e,Toast.LENGTH_SHORT).show();
}
}

并检查屏幕上是否出现带有错误的 Toast

Finnaly 使用 AsyncTask 检查响应代码的工作代码。

if (!GetServer1.isEmpty() || !GetServer1.equals("")) {
new MyTask().execute(GetServer1);
}
private class MyTask extends AsyncTask<String, Integer, String> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected String doInBackground(String... params) {
// get the string from params, which is an array
int StatusCode = 0;
try {
String string  = params[0];
url = new URL(string);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(3000);
urlConnection.setReadTimeout(3000);
urlConnection.setRequestMethod("GET");
urlConnection.setInstanceFollowRedirects(false);
urlConnection.connect();
StatusCode = urlConnection.getResponseCode();
} catch (Exception e) {
//Toast.makeText(Act_Details.this, ""+e, Toast.LENGTH_SHORT).show();
}
return String.valueOf(StatusCode);
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Do things like hide the progress bar or change a TextView
ResponseCode = result;
switch (result) {
case "200":
//Toast.makeText(Act_Details.this, result, Toast.LENGTH_SHORT).show();
ButtonS1.setEnabled(true);
ButtonS1.setText("ACTIVE");
break;
case "403":
ButtonS1.setText("BLOCKED");
break;
case "404":
ButtonS1.setText("DOWN");
break;
default:
ButtonS1.setText("UNKNOWN / BROKEN");
break;
}
}
}

从这里

最新更新