我正在尝试在此链接上编译代码。
该代码不起作用,因为它被挂断在这一行:
textIn.setText(dataInputStream.readUTF());
出于某种原因,它没有挂断,writeUTF()
但它被挂断了readUTF()
。
谁能在这里指出我正确的方向?
这是我的代码:
public Socket socket = null;
public DataOutputStream dataOutputStream = null;
public DataInputStream dataInputStream = null;
public Thread readjsonthrd = new Thread(new ReadJSONThread());
private final static String LOG_TAG = AndroidClient.class.getSimpleName();
private final Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(LOG_TAG, "Before OnCreate() Try");
try {
Log.e(LOG_TAG, "In OnCreate() Try");
socket = new Socket("23.23.175.213", 9000);
Log.e(LOG_TAG, "Created Socket");
dataOutputStream = new DataOutputStream(socket.getOutputStream());
Log.e(LOG_TAG, "Created DataOutputStream");
dataInputStream = new DataInputStream(socket.getInputStream());
Log.e(LOG_TAG, "Created DataInputStream");
Profile p = new Profile();
Log.e(LOG_TAG, "Created Profile Instance");
//Gets the local profile via JSON and converts into Profile type
Gson gson = new Gson();
Log.e(LOG_TAG, "Created Gson Instance");
p = gson.fromJson(p.getProfileJSONStr(), Profile.class);
Log.e(LOG_TAG, "Converted Profile to JSON");
//Gson gson = new Gson();
Log.e(LOG_TAG, "Before: outputJSON = gson.toJson(p);");
outputJSON = gson.toJson(p);
Log.e(LOG_TAG, "Created outputJSON");
dataOutputStream.writeUTF(outputJSON); //OUTPUT OF JSON FROM LOCAL PROFILE BEING SENT TO THE SERVER
dataOutputStream.flush();
Log.e(LOG_TAG, "Created dataOutputStream");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e(LOG_TAG, "Before initEventHandlers");
initEventHandlers();
Log.e(LOG_TAG, "Create Thread");
Thread serverthrd = new Thread(new ServerThread());
Log.e(LOG_TAG, "Start Thread");
serverthrd.start();
Log.e(LOG_TAG, "Started Thread");
}
public class ServerThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
//Socket socket = null;
//DataOutputStream dataOutputStream = null;
//DataInputStream dataInputStream = null;
Log.e(LOG_TAG, "Before Server Try Statement");
try {
Log.e(LOG_TAG, "In Server Try Statement");
//socket = new Socket("23.23.175.213", 1337);
//dataOutputStream = new DataOutputStream(socket.getOutputStream());
//dataInputStream = new DataInputStream(socket.getInputStream());
/*Profile p = null;
//Gets the local profile via JSON and converts into Profile type
Gson gson = new Gson();
p = gson.fromJson(p.getProfileJSONStr(), Profile.class);
//Gson gson = new Gson();
outputJSON = gson.toJson(p);
dataOutputStream.writeUTF(outputJSON); //OUTPUT OF JSON FROM LOCAL PROFILE BEING SENT TO THE SERVER
*/
//dataOutputStream.writeUTF(textOut.getText().toString()); //OUTPUT JSON GOES HERE
//textIn.setText(dataInputStream.readUTF());
Log.e(LOG_TAG, "Start Thread");
readjsonthrd.start();
Log.e(LOG_TAG, "Started Thread");
/*Log.e(LOG_TAG, "Before inputJSON String");
inputJSON = dataInputStream.readUTF();
//Convert
Log.e(LOG_TAG, "After inputJSON String");
Log.e(LOG_TAG, "InputJSON:" + inputJSON);*/
//textIn.setText(dataInputStream.readUTF()); //GET JSON COMING FROM SERVER HERE
refreshViewModels();
} /*catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class ReadJSONThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
//Socket socket = null;
//DataOutputStream dataOutputStream = null;
//DataInputStream dataInputStream = null;
Log.e(LOG_TAG, "Before Read JSON Try Statement");
try {
Log.e(LOG_TAG, "Before inputJSON String");
inputJSON = dataInputStream.readUTF();
//Convert
Log.e(LOG_TAG, "After inputJSON String");
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}