I'm currently developing an application using android-studio
with Kotlin
and an API system using django-rest-framework
.
I want to access my local server 127.0.0.1
with an android emulator to get some value from the API method.
I checked some questions in the stack overflow site and other sites and I set my codes up referring to some articles, like adding some code in an AndroidManifest.xml
, making a network_security_config.xml
, changing a proxy of Android emulator
and changing some code in Django rest framework
project.
even though when I hit an endpoint in my local server I have error codes like below:
2020-10-07 22:37:39.318 7247-7247/com.sample.apitest I/System.out: checked
2020-10-07 22:37:39.358 7247-7280/com.sample.apitest D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
2020-10-07 22:37:39.392 7247-7280/com.sample.apitest W/System.err: java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8000
2020-10-07 22:37:39.394 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)
2020-10-07 22:37:39.394 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)
2020-10-07 22:37:39.394 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)
2020-10-07 22:37:39.395 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
2020-10-07 22:37:39.395 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
2020-10-07 22:37:39.395 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
2020-10-07 22:37:39.396 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
2020-10-07 22:37:39.396 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
2020-10-07 22:37:39.396 7247-7280/com.sample.apitest W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
2020-10-07 22:37:39.397 7247-7280/com.sample.apitest W/System.err: at com.sample.apitest.MainActivity$HitAPITask.doInBackground(MainActivity.kt:49)
2020-10-07 22:37:39.397 7247-7280/com.sample.apitest W/System.err: at com.sample.apitest.MainActivity$HitAPITask.doInBackground(MainActivity.kt:39)
2020-10-07 22:37:39.397 7247-7280/com.sample.apitest W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
2020-10-07 22:37:39.397 7247-7280/com.sample.apitest W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2020-10-07 22:37:39.397 7247-7280/com.sample.apitest W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2020-10-07 22:37:39.397 7247-7280/com.sample.apitest W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2020-10-07 22:37:39.398 7247-7280/com.sample.apitest W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2020-10-07 22:37:39.398 7247-7280/com.sample.apitest W/System.err: at java.lang.Thread.run(Thread.java:764)
How could I solve this error?
Here are the codes:
Android project
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" package="com.sample.apitest">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
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"
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="n">
<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>
network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
MainActivity.kt
package com.sample.apitest
import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import org.json.JSONException
import org.json.JSONObject
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
class MainActivity : AppCompatActivity() {
var name: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
name = findViewById(R.id.textView)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
println("checked")
HitAPITask().execute("http://localhost:8000/api/name/")
}
}
inner class HitAPITask : AsyncTask<String, String, String>() {
override fun doInBackground(vararg params: String?): String? {
var connection: HttpURLConnection? = null
var reader: BufferedReader? = null
val buffer: StringBuffer
try {
val url = URL(params[0])
connection = url.openConnection() as HttpURLConnection
connection.connect()
val stream = connection.inputStream
reader = BufferedReader(InputStreamReader(stream))
buffer = StringBuffer()
var line: String?
while (true) {
line = reader.readLine()
if (line == null) {
break
}
buffer.append(line)
}
var jsonText = buffer.toString()
val jsonObj = JSONObject(jsonText)
val plant_name: String = jsonObj.getString("name")
return plant_name
} catch (e: MalformedURLException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} catch (e: JSONException) {
e.printStackTrace()
} finally {
connection?.disconnect()
try {
reader?.close()
} catch (e: IOException) {
e.printStackTrace()
}
}
return null
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
if (result == null) return
name!!.text = result
}
}
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.sample.apitest"
minSdkVersion 16
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"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.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'
}
Django rest framework project
project/settings.py
#...
INSTALLED_APPS = [
#...
'corsheaders',
]
MIDDLEWARE = [
#...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
CORS_ORIGIN_WHITELIST = [
'http://10.0.2.2:8000'
]
API endpoint
[ Method = GET ] http://127.0.0.1:8000/api/name/
{
"name": "myName.",
}
android-studio: 3.3.2
python: 3.7.5
django-cors-headers: 2.4.0
djangorestframework: 3.8.2
Try open cmd and then write
ipconfig
on it after that you got your local ip address then try to put on your codefor example you got 192.168.1.2 from your cmd then put it to your code like this: