0
このフラグメント(Android)では、メッセージGRを送信するとGR20のような回答を受け取る必要があります。コードは機能しません。 エラーが表示されない、いくつかの方法を試しましたが解決できません。 誰もが私を助けることができますか?はtextViewに値を書き込むことができません
package com.example.nicolarinaldi.myapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TabFragment2 extends Fragment {
private Socket socket;
// private static final int SERVERPORT = 60000;
//private static final String SERVER_IP = "172.17.8.104";
private static final int SERVERPORT = 60000;
private static final String SERVER_IP = "192.168.1.3";
int temperatura_termostato = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_fragment_2, container, false);
SeekBar seekBar = (SeekBar)rootView.findViewById(R.id.seekBar);
final TextView textView5 = (TextView)rootView.findViewById(R.id.textView5);
final TextView textView6 = (TextView)rootView.findViewById(R.id.textView6);
try {
new Thread(new ClientThread()).start();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.write("GR");
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String gi = in.readLine();
byte[] temperatura = gi.getBytes();
textView6.setText(String.valueOf(temperatura));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//textView6.setText(String.valueOf(temperatura));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
textView5.setText(String.valueOf(progress));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
new Thread(new ClientThread()).start();
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.write("SR" + temperatura_termostato);
out.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
return rootView;
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
"SR + temperatura termostato"の場合、このプログラムは "GR"のために機能し、noを受け取ります。なぜ私は理解できない –