I am a newbie to Android and Java, to a lesser extent, so forgive me if this question is ridiculous. **I am also sorry if this post is horribly formatted please understand I am new here, yes the instructions are everywhere but I don't know how to add follow up posts so I just edited the original post and added the new info I received.
I have an activity in an Android project that has to check if it can connect to a server. I simply have a button that when clicked will run code to check server connection.
When I click the button the application shuts down (Unfortunately .... has stopped).
If needed I can provide the full error log. Here is the code I have:
Notes: R.id.check_text refers to a TextView in the layout XML
I need this text to change given the results of isConnectedToServer method.
public class StartActivity extends Activity {
public static final int timeout = 3000;
public static final String TAG = "StartActivity";
public static final String url = "http://serverIP:port";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_start, menu);
return true;
}
public boolean isConnectedToServer(String url, int timeout) {
try {
URL serverURL = new URL(url);
URLConnection urlconn = serverURL.openConnection();
urlconn.setConnectTimeout(timeout);
urlconn.connect();
return true;
} catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
} catch (IllegalStateException e) {
Log.e(TAG, e.getLocalizedMessage(), e);
}
return false;
}
public void connectionReturn(View view) {
boolean a;
a = this.isConnectedToServer(url, timeout);
if (a == true) {
EditText edConnStatus = (EditText) findViewById(R.id.check_text);
edConnStatus.setText("Connection established");
} else {
EditText edConnStatus = (EditText) findViewById(R.id.check_text);
edConnStatus.setText("Connection to server could not be established");
}
}
}
And of course in my layout XML I have a Button that reads something like this:
<Button
android:id="@+id/bn_checkconnection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="connectionReturn"
android:text="@string/checkservconn" />
I appreciate any help that can be given and thank you all.
RECENT EDIT: I changed the connectionReturn method to have the parameters View view (ie. connectionReturn(View vw)) and saw a mistakes with onClick, now it calls the connectionReturn method. I don't get the same errors, I get new ones now! When I click the button the app freezes and Eclipse opens up Socket.class and that says:
Source not found The JAR file c:...\androidsdk\platforms\android17\android.jar has no source attachment Attach the source below....
AND
The Debug window view in Eclipse pops up with this:
Thread [<1> main] (Suspended (exception NetworkOnMainThreadException))
Socket.connect(SocketAddress, int) line: 849<-- It points to this immediately.
HttpConnection.(HttpConnection$Address, int) line: 76
HttpConnection.(HttpConnection$Address, int, HttpConnection$1) line: 50
HttpConnection$Address.connect(int) line: 340
HttpConnectionPool.get(HttpConnection$Address, int) line: 87
HttpConnection.connect(URI, SSLSocketFactory, Proxy, boolean, int) line: 128
HttpEngine.openSocketConnection() line: 316
HttpEngine.connect() line: 311
HttpEngine.sendSocketRequest() line: 290
HttpEngine.sendRequest() line: 240
HttpURLConnectionImpl.connect() line: 81
StartActivity.isConnectedToServer(String, int) line: 37
StartActivity.connectionReturn(View) line: 50
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
View$1.onClick(View) line: 3592
Button(View).performClick() line: 4202
View$PerformClick.run() line: 17340
Handler.handleCallback(Message) line: 725
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5039
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 793
ZygoteInit.main(String[]) line: 560
NativeStart.main(String[]) line: not available [native method]