を使用しているアプリケーションから自分のWebサイトに接続しようとしていますURLConnection
。キャッチUnknownHostException
しましたが、それでもアプリケーションがクラッシュし、logcatに次のエラーが表示されます。
12-22 17:17:21.992: W/System.err(2222): java.net.UnknownHostException: Unable to resolve host "mywebsite.com": No address associated with hostname
12-22 17:17:22.012: W/System.err(2222): at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
12-22 17:17:22.012: W/System.err(2222): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-22 17:17:22.026: W/System.err(2222): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-22 17:17:22.026: W/System.err(2222): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-22 17:17:22.026: W/System.err(2222): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-22 17:17:22.026: W/System.err(2222): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-22 17:17:22.026: W/System.err(2222): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-22 17:17:22.032: W/System.err(2222): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-22 17:17:22.032: W/System.err(2222): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
12-22 17:17:22.032: W/System.err(2222): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
12-22 17:17:22.042: W/System.err(2222): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
12-22 17:17:22.042: W/System.err(2222): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
12-22 17:17:22.042: W/System.err(2222): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
12-22 17:17:22.042: W/System.err(2222): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
12-22 17:17:22.052: W/System.err(2222): at com.example.myapp$loadMessageAsync.doInBackground(FreeSms.java:85)
12-22 17:17:22.052: W/System.err(2222): at com.example.myapp$loadMessageAsync.doInBackground(FreeSms.java:1)
12-22 17:17:22.062: W/System.err(2222): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-22 17:17:22.062: W/System.err(2222): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-22 17:17:22.072: W/System.err(2222): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-22 17:17:22.102: W/System.err(2222): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-22 17:17:22.102: W/System.err(2222): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-22 17:17:22.102: W/System.err(2222): at java.lang.Thread.run(Thread.java:856)
12-22 17:17:22.122: W/System.err(2222): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
12-22 17:17:22.122: W/System.err(2222): at libcore.io.Posix.getaddrinfo(Native Method)
12-22 17:17:22.132: W/System.err(2222): at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
12-22 17:17:22.132: W/System.err(2222): at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
12-22 17:17:22.132: W/System.err(2222): ... 21 more
12-22 17:17:22.142: W/System.err(2222): Caused by: libcore.io.ErrnoException: getaddrinfo failed: ETIMEDOUT (Connection timed out)
12-22 17:17:22.162: W/System.err(2222): ... 24 more
12-22 17:17:22.162: D/AndroidRuntime(2222): Shutting down VM
12-22 17:17:22.162: W/dalvikvm(2222): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-22 17:17:22.192: E/AndroidRuntime(2222): FATAL EXCEPTION: main
12-22 17:17:22.192: E/AndroidRuntime(2222): java.lang.NullPointerException
12-22 17:17:22.192: E/AndroidRuntime(2222): at com.example.myapp$loadMessageAsync.onPostExecute(FreeSms.java:101)
12-22 17:17:22.192: E/AndroidRuntime(2222): at com.example.myapp$loadMessageAsync.onPostExecute(FreeSms.java:1)
12-22 17:17:22.192: E/AndroidRuntime(2222): at android.os.AsyncTask.finish(AsyncTask.java:631)
12-22 17:17:22.192: E/AndroidRuntime(2222): at android.os.AsyncTask.access$600(AsyncTask.java:177)
12-22 17:17:22.192: E/AndroidRuntime(2222): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
12-22 17:17:22.192: E/AndroidRuntime(2222): at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 17:17:22.192: E/AndroidRuntime(2222): at android.os.Looper.loop(Looper.java:137)
12-22 17:17:22.192: E/AndroidRuntime(2222): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-22 17:17:22.192: E/AndroidRuntime(2222): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 17:17:22.192: E/AndroidRuntime(2222): at java.lang.reflect.Method.invoke(Method.java:511)
12-22 17:17:22.192: E/AndroidRuntime(2222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-22 17:17:22.192: E/AndroidRuntime(2222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-22 17:17:22.192: E/AndroidRuntime(2222): at dalvik.system.NativeStart.main(Native Method)
コード
public class MainActivity extends Activity {
private Button btnSkipContinue;
private TextView txtMessage,txtVersion;
private StringBuilder response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btnSkipContinue = (Button) findViewById(R.id.btnSkipCon);
btnSkipContinue.setVisibility(View.GONE);
txtMessage = (TextView) findViewById(R.id.txtMsgForUsers);
btnSkipContinue.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
Intent intent = new Intent(MainActivity.this, MyPage.class);
startActivity(intent);
}
});
if (isOnline()) {
try {
new loadMessageAsync().execute();
} catch (Exception ex) {
txtMessage.setText("Error while loading message. Cannot connect to server....");
btnSkipContinue.setVisibility(View.VISIBLE);
btnSkipContinue.setText("Continue");
}
} else {
Toast.makeText(getApplicationContext(), "No Internet Access", Toast.LENGTH_SHORT).show();
}
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
private class loadMessageAsync extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
URLConnection connection = new URL("http://mywebsite.com/new/Messages.aspx").openConnection();
connection.setConnectTimeout(60000);
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream responseStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(responseStream));
response = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
} catch (UnknownHostException ex) {
response.append("<html><body>Error while loading message. Cannot connect to server.</body></html>");
} catch (IOException ex) {
response.append("<html><body>Error while loading message. Cannot connect to server..</body></html>");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
try {
txtMessage.setText(Html.fromHtml(response.toString()));
} catch (Exception ex) {
txtMessage.setText("Error while loading message. Cannot connect to server...");
}
btnSkipContinue.setVisibility(View.VISIBLE);
btnSkipContinue.setText("Continue");
}
}
}