Reputation: 89
I want to check if a local website from my apache-server is up and reachable. If read through countless threads about HttpUrlconnection and came up with the following code. Manifest Internet Persmission is set. The website is currently running and I can access it via my smartphones webbrowser. I´ve also read about Input and Outputstreams, do I need them for my task?
public class MainActivity extends AppCompatActivity {
boolean value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
state(value);
}
public boolean state(boolean value){
try {
URL url = new URL("http://192.168.178.59:8090/");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("HEAD");
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
value = true;
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();
return value;
} catch (MalformedURLException e) {
e.printStackTrace();
value = false;
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
return value;
} catch (IOException e) {
e.printStackTrace();
value = false;
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
return value;
}
}
}
EDIT: Solution from Exception
import java.net.HttpURLConnection;
import android.os.AsyncTask;
public class MainActivity extends AppCompatActivity {
boolean value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new HttpTask().execute(value);
}
private class HttpTask extends AsyncTask<Boolean, Void, Boolean>
{
@Override
protected Boolean doInBackground(Boolean... params) {
// TODO Auto-generated method stub
boolean value=params[0];
try {
URL url = new URL("http://192.168.178.59:8090/");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("HEAD");
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
httpURLConnection.connect();
value = true;
//Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();
return value;
} catch (MalformedURLException e) {
e.printStackTrace();
value = false;
//Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
return value;
} catch (IOException e) {
e.printStackTrace();
value = false;
//Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
return value;
}
}
@Override
protected void onPostExecute(Boolean result) {
if(result){
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
}
}
}
}
EDIT: Service Solution
@Override
protected void onPostExecute(Boolean result) {
if(result){
Toast.makeText(NotifiyService.this,"true",Toast.LENGTH_SHORT).show();
//Notification in Status Bar
NotificationCompat.Builder builder = new NotificationCompat.Builder(NotifiyService.this);
builder.setSmallIcon(R.drawable.dummy);
Intent intent = new Intent(NotifiyService.this, Main22Activity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(NotifiyService.this,0,intent,0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dummy));
builder.setContentTitle(getResources().getString(R.string.newNotify));
builder.setContentText(getResources().getString(R.string.newNotify2));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
}
else{
Toast.makeText(NotifiyService.this,"false",Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Views: 144
Reputation: 2323
Replace your code with below given code.
import java.net.HttpURLConnection;
import android.os.AsyncTask;
public class MainActivity extends AppCompatActivity {
boolean value;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new HttpTask().execute(value);
}
private class HttpTask extends AsyncTask<Boolean, Void, Boolean>
{
@Override
protected Boolean doInBackground(Boolean... params) {
// TODO Auto-generated method stub
boolean value=params[0];
try {
URL url = new URL("http://192.168.178.59:8090/");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("HEAD");
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
httpURLConnection.connect();
value = true;
//Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();
return value;
} catch (MalformedURLException e) {
e.printStackTrace();
value = false;
//Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
return value;
} catch (IOException e) {
e.printStackTrace();
value = false;
//Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
return value;
}
}
@Override
protected void onPostExecute(Boolean result) {
if(result){
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_SHORT).show();
}
}
}
}
EDIT:
To launch new activity on notification click, add these lines after Intent intent = new Intent(NotifiyService.this, Main22Activity.class);
:
intent .setAction(Intent.ACTION_MAIN);
intent .addCategory(Intent.CATEGORY_LAUNCHER);
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Upvotes: 1
Reputation: 790
Simply run this code:-
public boolean state(boolean value){
URL url;
String response = "";
try {
url = new URL("http://192.168.178.59:8090/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
//writer.write(getPostDataString(postDataParams));
writer.write(value);
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
System.out.println(response);
}
} else {
response = "";
}
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1