I am working on socket programming. I am new to it. My application of socket programming works well in android 10 but when I try it with android 5.1 when I start communication from socket it crashes its working well in android 10
here is my code //mainactivity
package com.androidbegin.socket_programming;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class MainActivity extends AppCompatActivity{
public TextView messageFromServer;
public int packet_length;
public int track_id;
public int speed;
public int azimuth;
public int heading;
public int iff_status;
public int wco;
public int range;
public int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageFromServer = (TextView) findViewById(R.id.from_server);
thread.start();
}
final Handler handler = new Handler();
Thread thread = new Thread(new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void run () {
while(true) {
try {
Socket s = new Socket("192.168.126.188", 8054);
DataInputStream input=new DataInputStream(s.getInputStream());
byte[] bytes=new byte[15];
while((len = input.read(bytes)) != -1)
{
input.read(bytes);
packet_length=Byte.toUnsignedInt(bytes[0]);
track_id=Byte.toUnsignedInt(bytes[1]);
range=256*Byte.toUnsignedInt(bytes[2])*256 256*Byte.toUnsignedInt(bytes[3]) Byte.toUnsignedInt(bytes[4]);
height=256*Byte.toUnsignedInt(bytes[5]) Byte.toUnsignedInt(bytes[6]);
azimuth=256*Byte.toUnsignedInt(bytes[7]) Byte.toUnsignedInt(bytes[8]);
heading=256*Byte.toUnsignedInt(bytes[9]) Byte.toUnsignedInt(bytes[10]);
speed=256*Byte.toUnsignedInt(bytes[11]) Byte.toUnsignedInt(bytes[12]);
iff_status=bytes[13];
wco=bytes[14];
printValues();
handler.post(new Runnable() {
@Override
public void run() {
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
public void printValues()
{
messageFromServer.setText(range " " height " " azimuth " " heading " " speed " " iff_status " " wco);
}
}
code from build.gradle app file
plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.androidbegin.socket_programming"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4. '
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
I need help with this. I don't know why its crashes
CodePudding user response:
I think you've chosen Android 10 in options menu. When you create a new project, you have to chose the minimum android version for your android app. And, you can use Kotlin also.
CodePudding user response:
my suggestion is to set the minimum SDK to 23