我正在尝试在this链接处编译代码。
该代码无法正常工作,因为它已挂在此行上:
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();
}
}
}
最佳答案
与一个gov API集成时,我遇到了类似的问题,我使用的是readUTF()和writeUTF(),每次卡在readUTF()时都没有任何响应。
我发现服务器位于c中,并且期望结尾或请求字符串带有*,响应也以*结尾。
我通过直接在套接字的输入/输出流上写入并在遇到**时终止读取来解决此问题。
Socket client = new Socket(serverIp, port);
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();
String test = "REQUEST-STRING**";
out.write(test.getBytes());
out.flush();
int c;
while ((c=in.read())!=-1) {
if (isEndOfResponse(c)) {
break;
}
System.out.print((char)c);
}
client.close();