在此应用程序中,我想在应用程序开始运行时发送Web请求。您可以在此处查看我的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
geturl("https://www.google.com/");
}
// and my other codes...
我使用此功能发送我的Web请求。
public void geturl(String URL){
HttpURLConnection client = null;
try{
URL url = new URL(URL);
client = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(client.getInputStream());
Toast.makeText(this, in.toString(), Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
//bad URL, tell the user
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
//network error/ tell the user
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
} finally {
client.disconnect();
}
}
您可以在这里看到我的AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.up.applications">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
也是这是我的礼物。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "coin.up.ozvdarozv"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard- rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'org.apache.httpcomponents:httpcore:4.4.1'
implementation("com.squareup.okhttp:okhttp:2.7.2")
implementation 'com.android.volley:volley:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
android {
useLibrary 'org.apache.http.legacy'
}
当我启动我的应用程序时,它向我显示强制停止错误,并且我看不到任何Toast消息以了解我的应用程序中的错误和问题。
AsyncTask
在后台获取URL。private final class FetchUrl extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String response = "N/A"
HttpURLConnection client = null;
try{
URL url = new URL("https://www.google.com/");
client = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(client.getInputStream());
response = in.toString();
} catch (MalformedURLException e) {
} catch (IOException e) {
} finally {
client.disconnect();
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
// Here, 'result' is the response of "https://www.google.com/".
// Update your UI here.
}
}
onCreate
方法:@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new FetchUrl().execute();
}