dimanche 4 janvier 2015

Socket Programming with Broadcast Recevier


I am developing Android application(online marketing), within this project i am using socket programming for communication with server, when i communicate with server if server does not give me any response within 30 sec then my asynctask will close automatically, but now problem is i want to save server response in sqlite on receiving response from server please provide me the necessary help thank you in advance.


Here is My Code



public class SendToserver extends AsyncTask<Integer, Integer, Integer>
{
int iReturn;

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progressDialog(Registartion.this, "Please Wait...");
super.onPreExecute();
}

@Override
protected Integer doInBackground(Integer... params) {
// TODO Auto-generated method stub

ServerData = runm(IMEINo);
if (ServerData.equalsIgnoreCase("IsNull"))
{
iReturn = 2;
}
else {
iReturn = 1;
}

return iReturn;
}

@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
if (iReturn == 1)
{
String ServerIP = edtServerIpAddress.getText().toString();
String ServerPort = edtServerPort.getText().toString();

registartionModel = new RegistartionModel();
registartionModel.setServerIPAdreess(ServerIP);
registartionModel.setPort(ServerPort);

registrationDb = new RegistrationDb(getApplicationContext());
registrationDb.open();
registrationDb.insertData(registartionModel);
registrationDb.close();

Intent i = new Intent(getBaseContext(), LoginActivity.class);
startActivity(i);
Registartion.this.finish();
System.gc();

}
else if (iReturn == 2)
{
showDialogBox("Please Input Correct Server IP and Port");
}

super.onPostExecute(result);
}

}


My Server Communication Method(runm) :



@SuppressLint("NewApi")
public String runm(String DId)
{
try {
socket = new Socket(SERVER_IP, SERVERPORT);
out = new PrintStream(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write((DId + "|" +"REG").getBytes());
ServerData = in.readLine();
System.err.println("Server Data in Runm :- " + ServerData);

if(ServerData == null || ServerData.equalsIgnoreCase(""))
{
ServerData="IsNull";
}

}
catch (NetworkOnMainThreadException e)
{
ServerData = "IsNull";
}catch (IOException ex) {
ServerData = "IsNull";
}
return ServerData;
}




Aucun commentaire:

Enregistrer un commentaire