我正在尝试在此链接上编译代码。
代码不起作用,因为它挂在这条线上:
textIn.setText(dataInputStream.readUTF());
Run Code Online (Sandbox Code Playgroud)
出于某种原因,它没有挂断,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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在与一个 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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4428 次 |
| 最近记录: |