我在计算机上创建Java服务器,并在Android应用程序中创建客户端。一切都很好,但是突然客户端停止了向服务器发送数据。客户端通常会发送3到5次数据,但有时它不再起作用。
我为客户使用意向服务。 我没有添加Java服务器,因为我认为服务器没有问题。
我的意向服务
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();
}
}