I've been trying some code in this link here. It is in the part how to connect android to python with flask. The code running well in the server. When i try to run the android application, it work well untill when i click the "connect to server" button, the result is always failed to connect. There are no error in the android studio
This is the python code
import flask
app = flask.Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def handle_request():
return "Flask Server & Android are Working Successfully"
app.run(host="0.0.0.0", port=5000, debug=True)
This is the terminal in the python code.
* Serving Flask app "flaskcoba1" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: 189-748-501
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
I tried to do test connection using ping from cmd to my mobile device to check the connection, and it's connect.
This is the main.activity
package com.example.flaskcoba;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void connectServer(View v){
EditText ipv4AddressView = findViewById(R.id.IPAddress);
String ipv4Address = ipv4AddressView.getText().toString();
EditText portNumberView = findViewById(R.id.portNumber);
String portNumber = portNumberView.getText().toString();
String postUrl= "http://"+ipv4Address+":"+portNumber+"/";
String postBodyText="Hello";
MediaType mediaType = MediaType.parse("text/plain; charset=utf-8");
RequestBody postBody = RequestBody.create(mediaType, postBodyText);
postRequest(postUrl, postBody);
}
public void postRequest(String postUrl, RequestBody postBody) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
responseText.setText("Failed to Connect to Server");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
try {
responseText.setText(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
}
This is the layout code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="IPv4 Address" />
<EditText
android:layout_weight="4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/IPAddress"
android:text="192.168.1.7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Port Number" />
<EditText
android:layout_weight="4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/portNumber"
android:text="5000"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connect to Server"
android:onClick="connectServer"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message from the Server ..."
android:id="@+id/responseText" />
</LinearLayout>
Finally find the error inthe logcat
2020-06-29 13:28:46.182 3001-3001/? E/IptablesRestoreController: [iptables debug]iptables-restore execute *filter
-nvx -L tetherctrl_counters
COMMIT
2020-06-29 13:28:46.186 3001-3001/? E/IptablesRestoreController: [iptables debug]iptables-restore execute done, res : 0
2020-06-29 13:28:46.186 3001-3001/? E/IptablesRestoreController: [iptables debug]iptables-restore execute *filter
-nvx -L tetherctrl_counters
COMMIT
2020-06-29 13:28:46.190 3001-3001/? E/IptablesRestoreController: [iptables debug]iptables-restore execute done, res : 0
2020-06-29 13:28:57.283 3287-3768/? E/Watchdog: !@Sync 718 [2020-06-29 13:28:57.283] FD count : 517
2020-06-29 13:31:46.659 3645-4181/? E/ImsAdaptorImpl: setSSACInfo : ImsAdaptorImpl.
2020-06-29 13:31:46.661 3889-4332/? E/EPDG -- SIM0 [RILRECEIVER]: Incorrect EpdgIilIpcMessage IPC Message -- not initialized
2020-06-29 13:31:46.663 3645-4181/? E/ImsAdaptorImpl: setSSACInfo : ImsAdaptorImpl.
2020-06-29 13:31:46.664 3889-4332/? E/EPDG -- SIM0 [RILRECEIVER]: Incorrect EpdgIilIpcMessage IPC Message -- not initialized
2020-06-29 13:31:46.670 3889-4332/? E/EPDG -- SIM0 [RILRECEIVER]: RX [NET_REGIST] -- RESPONSE -- not initialized
Please help me to fix the error