的readUTF()导致Android应用程序挂起 [英] readUTF() causing Android app to hang
问题描述
我试图编译code。在此链接。
在code未工作,因为它是越来越挂上这一行:
textIn.setText(dataInputStream.readUTF());
出于某种原因,它不会挂在 writeUTF()
,但它被挂在的readUTF()
。
任何人都可以点我在这里正确的方向?
下面是我的code:
公共Socket套接字= NULL;
公共DataOutputStream类DataOutputStream类= NULL;
公共DataInputStream以DataInputStream以= NULL;
公共主题readjsonthrd =新主题(新ReadJSONThread());私人最终静态字符串LOG_TAG = AndroidClient.class.getSimpleName();
私人最终处理程序处理程序=新的处理程序();@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); Log.e(LOG_TAG,在OnCreate()之前尝试);
尝试{
Log.e(LOG_TAG,在OnCreate()中试);
插座=新的Socket(23.23.175.213,9000);
Log.e(LOG_TAG,创建的套接字);
DataOutputStream类=新的DataOutputStream类(socket.getOutputStream());
Log.e(LOG_TAG,创建DataOutputStream类);
DataInputStream以=新DataInputStream所(socket.getInputStream());
Log.e(LOG_TAG,创建DataInputStream所); 档案P =新的配置文件();
Log.e(LOG_TAG,创建配置文件实例); //通过JSON获取本地配置文件,并转换成配置文件类型
GSON GSON =新GSON();
Log.e(LOG_TAG,创建GSON实例);
p值= gson.fromJson(p.getProfileJSONStr(),Profile.class);
Log.e(LOG_TAG,转换配置文件以JSON); // GSON GSON =新GSON();
Log.e(LOG_TAG,前:outputJSON = gson.toJson(对););
outputJSON = gson.toJson(对);
Log.e(LOG_TAG,创建outputJSON); dataOutputStream.writeUTF(outputJSON); // JSON从本地配置文件输出端发送到服务器
dataOutputStream.flush();
Log.e(LOG_TAG,创建DataOutputStream类);
}赶上(UnknownHostException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
} Log.e(LOG_TAGinitEventHandlers之前);
initEventHandlers();
Log.e(LOG_TAG,创建主题);
螺纹serverthrd =新主题(新ServerThread()); Log.e(LOG_TAG,启动线程);
serverthrd.start();
Log.e(LOG_TAG,启动线程);
}公共类ServerThread实现Runnable {
@覆盖
公共无效的run(){
// TODO自动生成方法存根
// Socket套接字= NULL;
// DataOutputStream类DataOutputStream类= NULL;
// DataInputStream以DataInputStream以= NULL;
Log.e(LOG_TAG,服务器try语句之前);
尝试{
Log.e(LOG_TAG,在服务器try语句);
//插座=新的Socket(23.23.175.213,1337); // DataOutputStream类=新的DataOutputStream类(socket.getOutputStream());
// DataInputStream以=新DataInputStream所(socket.getInputStream());
/ *档案P = NULL; //通过JSON获取本地配置文件,并转换成配置文件类型
GSON GSON =新GSON();
p值= gson.fromJson(p.getProfileJSONStr(),Profile.class); // GSON GSON =新GSON();
outputJSON = gson.toJson(对);
dataOutputStream.writeUTF(outputJSON); // JSON从本地配置文件输出端发送到服务器
* /
。//dataOutputStream.writeUTF(textOut.getText()的toString()); //输出JSON GOES HERE
//textIn.setText(dataInputStream.readUTF());
Log.e(LOG_TAG,启动线程);
readjsonthrd.start();
Log.e(LOG_TAG,启动线程);
/*Log.e(LOG_TAG,inputJSON字符串之前);
inputJSON = dataInputStream.readUTF(); //兑换
Log.e(LOG_TAG,inputJSON字符串后面);
Log.e(LOG_TAGInputJSON:+ inputJSON); * / //textIn.setText(dataInputStream.readUTF()); //获取JSON COMING FROM HERE服务器
refreshViewModels();
} / *赶上(UnknownHostException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
} * /
最后{
如果(插座!= NULL){
尝试{
socket.close();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
} 如果(DataOutputStream类!= NULL){
尝试{
dataOutputStream.close();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
} 如果(DataInputStream以!= NULL){
尝试{
dataInputStream.close();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}
}
}公共类ReadJSONThread实现Runnable {
@覆盖
公共无效的run(){
// TODO自动生成方法存根
// Socket套接字= NULL;
// DataOutputStream类DataOutputStream类= NULL;
// DataInputStream以DataInputStream以= NULL;
Log.e(LOG_TAG,读JSON try语句之前);
尝试{
Log.e(LOG_TAG,inputJSON字符串之前);
inputJSON = dataInputStream.readUTF(); //兑换
Log.e(LOG_TAG,inputJSON字符串后面);
}
赶上(UnknownHostException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
}
我遇到有些类似的问题,而一个政府网站的API集成,我是用的readUTF()writeUTF(),并在的readUTF每次stucks()没有任何反应。
我发现服务器在c和期待年底或请求字符串带*的,也反应与* 的结束。
我解决了这个,通过插座上的输入/输出流直接写入,和**的时候遇到终止读数。
客户端的Socket =新的Socket(SERVERIP,口);
OutputStream的OUT = client.getOutputStream();
InputStream的时间= client.getInputStream(); 字符串测试=请求字符串**;
out.write(test.getBytes());
了out.flush(); INT℃;
而((C = in.read())!= - 1){
如果(isEndOfResponse(c))的{
打破;
}
System.out.print((char)的C);
}
client.close();
I'm trying to compile the code at this link.
The code isn't working because it is getting hung up on this line:
textIn.setText(dataInputStream.readUTF());
For some reason it doesn't get hung up on writeUTF()
but it gets hung up on readUTF()
.
Can anyone point me in the right direction here?
Here is my code:
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();
}
}
}
I faced somewhat similar issue while integrating with one gov API, I was using readUTF() writeUTF(), and every time stucks at readUTF() without any response.
I found the server is in c and expecting end or request string with *, also response ends with *.
I resolved this, by directly writing on socket's input/output stream, and terminated reading when ** encountered.
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();
这篇关于的readUTF()导致Android应用程序挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!