Я создаю сервер Java на моем компьютере и клиент в моем приложении Android. Все шло нормально, но вдруг клиент перестал отправлять данные на сервер. Часто клиент отправляет данные от 3 до 5 раз, но иногда это больше не работает.
Я использую Intent Service для клиента.
Я не добавляю свой Java-сервер, потому что я не думаю, что есть проблема с сервером.
Служба My Intent
package com.example.finch.ornek_proje;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Objects;
public class MyIntentService extends IntentService {
private static final String TAG = MyIntentService.class.getSimpleName();
String [] trend_list;
String file_size;
int filesize;
int read2 = 0;
int totalRead = 0;
int remaining;
byte [] bytes;
private static final int SERVERPORT = 2001;
private static final String SERVER_IP = "192.168.1.45";
public MyIntentService() {
super("MyIntentService");
}
@Override
public void onCreate(){
Log.i(TAG,"starting");
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Socket socket = new Socket(serverAddr, SERVERPORT);
InputStream input_stream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(input_stream);
BufferedReader input = new BufferedReader(new InputStreamReader(input_stream));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println("get_trend_list:0");
String read = input.readLine();
String [] infos;
trend_list = read.split(",");
for (int b = 0;b < 10; b++){
out.println("get_vd_info_less:"+ trend_list[b]);
read = input.readLine();
infos = read.split(",");
}
out.println("shutdown:0");
dataInputStream.close();
input_stream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bytes = new byte[4096];
for (int b = 0;b < 10; b++){
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Socket socket = new Socket(serverAddr, SERVERPORT);
InputStream input_stream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(input_stream);
BufferedReader input = new BufferedReader(new InputStreamReader(input_stream));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println("get_vd_img1:"+ trend_list[b]);
file_size = input.readLine();
filesize = Integer.valueOf(file_size);
remaining = filesize;
String filepath = getFilesDir().getPath() +"img"+Integer.toString(b)+".png";
File file = new File(filepath);
OutputStream outputStream = new FileOutputStream(file);
while ((read2 = dataInputStream.read(bytes, 0, Math.min(bytes.length, remaining))) > 0){
totalRead += read2;
remaining -= read2;
outputStream.write(bytes,0,read2);
}
outputStream.close();
filesize = 0;
read2 = 0;
totalRead = 0;
remaining = 0;
bytes = new byte[4096];
out.println("shutdown:0");
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bytes = new byte[4096];
for (int b = 0;b < 10; b++){
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
Socket socket = new Socket(serverAddr, SERVERPORT);
InputStream input_stream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(input_stream);
BufferedReader input = new BufferedReader(new InputStreamReader(input_stream));
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
Log.d("abcv", String.valueOf(b));
out.println("get_vd_img2:"+ trend_list[b]);
Log.d("abcv", String.valueOf(b));
file_size = input.readLine();
filesize = Integer.valueOf(file_size);
remaining = filesize;
String filepath = getFilesDir().getPath() +"img"+Integer.toString(b+10)+".png";
File file = new File(filepath);
OutputStream outputStream = new FileOutputStream(file);
Log.d("abc","abc");
Log.d("abcv", String.valueOf(filesize));
while ((read2 = dataInputStream.read(bytes, 0, Math.min(bytes.length, remaining))) > 0){
totalRead += read2;
remaining -= read2;
outputStream.write(bytes,0,read2);
Log.d("abcv", String.valueOf(b));
}
Log.d("abcv", String.valueOf(b));
outputStream.close();
filesize = 0;
read2 = 0;
totalRead = 0;
remaining = 0;
bytes = new byte[4096];
out.println("shutdown:0");
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy(){
Log.i(TAG,"closing");
super.onDestroy();
}
}