Mi aplicación ha dejado de funcionar (Fuerza Stop) mientras se envían solicitud Web HTTP

Mohammad Rajabi:

En esta aplicación, quiero enviar una solicitud web, mientras que la aplicación comienza a work.You puede ver mi código aquí abajo:

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...

Yo uso esta función para enviar mi solicitud 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();
    }
}

Usted puede ver mi AndroidManifest.xmlaquí abajo:

<?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>

Y también esto es mi Gradle.

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

Cuando inicio mi aplicación me muestra un error de la fuerza de parada y no veo ningún mensaje de aviso emergente para comprender el error y el problema en mi aplicación.

mubin986:

Usted debe buscar la dirección URL en un hilo separado.
Utilice AsyncTaska buscar la dirección URL en el fondo.

  • Ponga esta clase en su misma clase de actividad:
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.
    }
}
  • Actividad del onCreatemétodo:
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   new FetchUrl().execute();
}

Supongo que te gusta

Origin http://10.200.1.11:23101/article/api/json?id=385564&siteId=1
Recomendado
Clasificación