0
Guys, I had already opened a question related to this subject but in java(Here), I could not solve and now in an android apk I have the same problem.
I have a service with a thread running a server socket, in it using Resultreceiver game information to a textview, these information are String’s of a java client on the pc, for effect of manipulating this received data using a switch want to convert the String to an integer(If you have another way...).
However in the conversation using the parseint method the application of the error, I know that I am programming a pc with less than 1gb of ram, can not open the emulator so little see the error log. Besides how the socket connection the eclipse emulator has no ability to emulate wifi. I know it was a mistake when it came to converting because I commented on this excerpt and the program ran normal.
I do not know if this is the correct way to manipulate the received data, but I believe that it does not disturb anything the main thread;
If anyone can test and see why the conversation is unsuccessful, or if someone already knows the problem by looking at the code and can identify, the codes are following:
Here is the service class that runs the thread with the socket server
package com.example.palioteste;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
public class ServidorSocketIntent extends IntentService{
public int porta = 9955;
public Socket conexao;
public boolean ativo;
public String clientSentence;
private ResultReceiver rr;
public ServidorSocketIntent() {
super("ServidorSocketIntentThread");
ativo = true;
clientSentence = "";
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
rr = intent.getParcelableExtra("receiver");
return(super.onStartCommand(intent, flags, startId));
}
@Override
protected void onHandleIntent(Intent intent) {
ServerSocket welcomeSocket = null;
try {
welcomeSocket = new ServerSocket(porta);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (ativo) {
try{
Socket connectionSocket = null;
conexao = welcomeSocket.accept();
BufferedReader inFromClient = null;
inFromClient = new BufferedReader(new InputStreamReader( conexao.getInputStream() ));
clientSentence = inFromClient.readLine();
//clientSentence = inFromClient.readLine();
Bundle b = new Bundle();
b.putString("resposta", clientSentence);
rr.send(1, b);
} catch(Exception e ){
}
try {
conexao.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Here the Mainactivity:
package com.example.palioteste;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements ServiceConnection{
private ServiceConnection connection;
private RespostaCliente respostafinal;
private ImageButton imageButtonLigar;
private ImageButton imageButtonDesligar;
static TextView feedback;
private ResultReceiverListener rr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connection = this;
imageButtonLigar = (ImageButton) findViewById(R.id.imageButtonLigar);
imageButtonDesligar = (ImageButton) findViewById(R.id.imageButtonDesligar);
feedback = (TextView) findViewById(R.id.feedback);
imageButtonLigar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
abrirSocket rodar = new abrirSocket("2");
rodar.execute();
imageButtonLigar.setVisibility(View.INVISIBLE);
imageButtonDesligar.setVisibility(View.VISIBLE);
}
});
imageButtonDesligar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
abrirSocket rodar = new abrirSocket("3");
rodar.execute();
imageButtonLigar.setVisibility(View.VISIBLE);
imageButtonDesligar.setVisibility(View.INVISIBLE);
}
});
//bindService(new Intent("Socket_Resposta"),connection , Context.BIND_AUTO_CREATE);//Context.BIND_AUTO_CREATE
//inicia o servico binder para obter conexao com a thread rodando dentro do servico
startService();
}
public void startService(){
rr = null;
rr = new ResultReceiverListener(null);
Intent it = new Intent ("Socket_RespostaIntent");
it.putExtra("receiver", rr);
startService(it);
}
public void stopService (View view){
Intent it = new Intent ("Socket_RespostaIntent");
stopService(it);
it.putExtra("desligar", 1);
startService(it);
}
/*public void startService(View view){
Intent it = new Intent("Servico_Teste");
startService(it);
}
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
private class ResultReceiverListener extends ResultReceiver{
public ResultReceiverListener(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultcode, Bundle bundle){
if (resultcode == 1){
final String resposta = bundle.getString("resposta");
//metodo que recebe a resposta e atualiza a ui
runOnUiThread(new Runnable(){
public void run(){
// feedback.setText("Resposta do Cliente: "+ resposta);
int respostaint = Integer.parseInt(resposta);
switch(respostaint){
case 1:
feedback.setText("O resultado e 1");
\\executa outros metodos
break;
case 2:
feedback.setText("Resposta 2");
\\executa outras funcoes
break;
}
}
});
}
}
}
}
Here the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.palioteste"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:label="ServidorSocket" android:name="ServidorSocket">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="Socket_Resposta"/>
</intent-filter>
</service>
<service android:label="ServidorSocketIntent" android:name="ServidorSocketIntent">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="Socket_RespostaIntent"/>
</intent-filter>
</service>
</application>
</manifest>
&& !sNum.equals("")
– Edson Reis
Also, if you need
– GabrielLocalhost
Thanks for the contact people, I had already tested these methods until in Java, however they did not work, curious and that for example, in java when using parseint, there is no error in the log, however the result is not as expected https://answall.com/questions/243020/returnunexpected-to-convert-string-to-full-to-java. On Android there is no error in the compilation either, however the result is not correct, see now here https://answall.com/questions/243794/ler-integer.
– Aislan Silva Costa
To clarify using only x != null the returned value is always zero(0), using != null and ! x.equals("") result and null does not appear anything. in both cases I put an Else if (x == null) z = 0000; even then it was accounted for zero(0) instead of 0000
– Aislan Silva Costa
If z is an int, 0000 is equal to zero and a sysout of z would be 0.
– mari
I already solved, in the java socket client for the output I used write(byte), and in the socket server I used read(), so it was not necessary any conversation, in the client socket use writeInt() not solved.
– Aislan Silva Costa