Jump to content

Need Help with android studio project

Hello,

 

I have been working on android studio project which uses a REST template. It keeps giving me an AsyncTask Error. It seems like it wont connect to internet.

 

Main

 

package al.programming.erlisciko.avp_workplez;

import android.os.AsyncTask;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String android_id = Settings.Secure.getString(this.getContentResolver(),
                Settings.Secure.ANDROID_ID);
        // The connection URL
        AsyncTask<String, Void, ResponseEntity<Object>> task = new RestTask().execute("https://jsonplaceholder.typicode.com/posts/1");

        Object result = new Object();
        try {
            result = task.get().getBody();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
// Make the HTTP GET request, marshaling the response to a String

        final TextView textview=(TextView) findViewById(R.id.textview);
        final TextView loltext=(TextView) findViewById(R.id.loltext);
        textview.setText(android_id);
        loltext.setText(result.toString());

    }




}

class RestTask extends AsyncTask<String, Void, ResponseEntity<Object>> {


    @Override
    protected ResponseEntity<Object> doInBackground(String... params) {
        RestTemplate restTemplate = new RestTemplate();
        //GET
        String url
                = "https://jsonplaceholder.typicode.com/posts/1";
        HttpEntity<String> entity = new HttpEntity<String>("parameters");

        ResponseEntity<Object> result
                = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class);

        return result;
    }

    @Override
    protected void onPostExecute(ResponseEntity<Object> deviceResponseEntity) {
        Object device = deviceResponseEntity.getBody();
    }
}

 

Manifest

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="al.programming.erlisciko.avp_workplez">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <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>

</manifest>

 

Gradle

 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "al.programming.erlisciko.avp_workplez"
        minSdkVersion 9
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
}
repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
}

 

Thank You in advance

TeamLTG

Link to comment
https://linustechtips.com/topic/898873-need-help-with-android-studio-project/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×