gpt4 book ai didi

Android HttpUrlConnection 简单网站连接

转载 作者:行者123 更新时间:2023-11-29 20:06:17 26 4
gpt4 key购买 nike

我想检查我的 apache-server 中的本地网站是否已启动并且可以访问。如果阅读了无数关于 HttpUrlconnection 的线程并提出了以下代码。 list Internet 权限已设置。该网站目前正在运行,我可以通过我的智能手机网络浏览器访问它。我还阅读了有关输入和输出流的内容,我的任务需要它们吗?

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;
}
}
}

编辑:异常解决方案

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();
}
}

}

}

编辑:服务解决方案

    @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();
}

最佳答案

用下面给定的代码替换您的代码。

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();
}
}

}

}

编辑:

要在通知点击时启动新 Activity ,请在 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);

关于Android HttpUrlConnection 简单网站连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35623028/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com