0
在此Fragment(Android)中,當我發送消息GR時,我必須收到類似GR20的答案,並且我希望在textView中將其可視化。該代碼不起作用。 我看不到錯誤,我嘗試了一些方法,但我無法解決。 任何人都可以幫助我嗎?無法在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 +溫度termostato」這個程序工作,但對於「GR」,並收到no。我不明白爲什麼 –