非法参数 HttpURLConnection
Illegal Argument HttpURLConnection
我是 android 的新手。我现在正在学习 android 网络。我正在尝试创建与 HttpURLConnection 的连接以将响应代码跟踪为 200,
但我收到 IllegalArgumentException。我正在使用异步任务执行此操作但无法纠正。任何帮助,将不胜感激。
这是我的代码:
package com.movies.usman.moviesmesh;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CheckConnectionStatus().execute("http://google.com");
}
class CheckConnectionStatus extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
URL url = null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//Log.i("Reponse: ", String.valueOf(urlConnection.getResponseCode()));
return String.valueOf(urlConnection.getResponseCode());
} catch (IOException e) {
Log.e("Error: ", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
text.setText(s);
}
}
}
您应该在 onCreate 中初始化您的 TextView
text = (TextView) findViewById(R.id.yourViewId);
在
之前
new CheckConnectionStatus().execute("http://google.com");
我是 android 的新手。我现在正在学习 android 网络。我正在尝试创建与 HttpURLConnection 的连接以将响应代码跟踪为 200, 但我收到 IllegalArgumentException。我正在使用异步任务执行此操作但无法纠正。任何帮助,将不胜感激。
这是我的代码:
package com.movies.usman.moviesmesh;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new CheckConnectionStatus().execute("http://google.com");
}
class CheckConnectionStatus extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
URL url = null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//Log.i("Reponse: ", String.valueOf(urlConnection.getResponseCode()));
return String.valueOf(urlConnection.getResponseCode());
} catch (IOException e) {
Log.e("Error: ", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
text.setText(s);
}
}
}
您应该在 onCreate 中初始化您的 TextView
text = (TextView) findViewById(R.id.yourViewId);
在
之前new CheckConnectionStatus().execute("http://google.com");