AsyncTask. Problems with receiving from Java Server - android

I want to use AsyncTask for receiving ArrayList's(in this case) from Java server. To be sure, that I received something from server I'm trying to display it with Toast.
The Code is following:
public class MainActivity extends Activity {
private DataReceiving dRec;
private DataTransfer dTrans;
private EditText inputData;
private Button sendParametersBtn;
private Button startComputationBtn;
private TextView displayText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputData=(EditText) findViewById(R.id.InputText);
sendParametersBtn=(Button) findViewById(R.id.button1);
startComputationBtn=(Button) findViewById(R.id.button2);
displayText=(TextView) findViewById(R.id.textView1);
sendParametersBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dRec = new DataReceiving();
dRec.execute();
}
});
private class DataReceiving extends AsyncTask<Void, Void, ArrayList>
{
#Override
protected ArrayList doInBackground(Void... params) {
ArrayList b = new ArrayList();
try {
b = receive();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
protected void onPostExecute(ArrayList result) {
super.onPostExecute(result);
Toast toast=Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT);
toast.show();
}
public ArrayList receive () throws IOException, ClassNotFoundException
{
ServerSocket s= new ServerSocket(8888);
Socket incoming =s.accept();
ObjectInputStream ios = new ObjectInputStream(incoming.getInputStream());
ArrayList b = (ArrayList) ios.readObject();
ios.close();
incoming.close();
s.close();
return b;
}
While clicking the sendParametersBtn nothing happening.
P.S. I can successfully transmit from Android to Server. So its not a connection or permission problem.
Thank you for help

Hi If your getting some thing from server you have to call web server url for fetching data. After data arrives response have some type it will JSON/XML if they are restful services if they are SOAP services they are in envelope. So after response return get that and parse them as per logic.
Look for HTTP get/post (for ping to server and get data )and parsing (JSON/XML).

Figured out! I removed receive method into doInBackground.

Related

android - changing Activity UI from application class

I extended the Application class in order to create singleton-like object in android.
in this object I have all the HTTP work with my server, and all the other activities can access it and call methods to GET, POST etc.
Code:
public class HttpManagerInstance extends Application {
private HttpClient httpClient;
private HttpGet get;
#Override
public void onCreate() {
httpClient = new DefaultHttpClient();
get = new HttpGet("http://10.100.102.9:8000/users/");
super.onCreate();
}
public Void getUsers() throws Exception {
new executeRequest().execute(get);
return null;
}
private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {
#Override
protected Integer doInBackground(HttpRequest... params) {
// TODO Auto-generated method stub
HttpRequest request = params[0];
HttpResponse response;
String result="";
try {
response = httpClient.execute((HttpUriRequest) request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseCode;
}
#Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
switch (result) {
case HttpStatus.SC_OK:
// request was fine
// Here I want to updated the GUI of the activity that called this method.
break;
}
}
}
}
This is how I call the method from the Activity:
HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
sampleApp.getUsers();
Again - I want to access the UI of the Activity that called the method to put an REQUEST ACCEPTED message.
Maybe pass a context? any ideas?
I'd create a listener:
public class HttpManagerInstance extends Application {
private HttpClient httpClient;
private HttpGet get;
public interface ResponseListener{
public void onSuccess(Object data);
}
#Override
public void onCreate() {
httpClient = new DefaultHttpClient();
get = new HttpGet("http://10.100.102.9:8000/users/");
super.onCreate();
}
public Void getUsers(ResponseListener listener) throws Exception {
new executeRequest(listener).execute(get);
return null;
}
private class executeRequest extends AsyncTask<HttpRequest, Void, Integer> {
private ResponseListener mListener;
public executeRequest(ResponseListener listener){
this.mListener = listener;
}
#Override
protected Integer doInBackground(HttpRequest... params) {
// TODO Auto-generated method stub
HttpRequest request = params[0];
HttpResponse response;
String result="";
try {
response = httpClient.execute((HttpUriRequest) request);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseCode;
}
#Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
switch (result) {
case HttpStatus.SC_OK:
// request was fine
// Here I want to updated the GUI of the activity that called this method.
if(this.mListener != null) mListener.onSuccess(whatEverDataYouWant);
break;
}
}
}
}
Then, in your activity:
HttpManagerInstance sampleApp = (HttpManagerInstance)getApplicationContext();
sampleApp.getUsers(new ResponseListener(){
public void onSuccess(Object data){
//update your ui!
}
});
The short answer is you can't directly reference to the UI from another activity. My advice would be for you to set up a callback on your Application class and call in on executeRequest#onPostExecute then implement that callback on your Activity and update your UI from there.
If you need help to implement the callback check this question
If you need to show message is good option the Dialog Class or the Toast Class, you can see more info are here:
Dialogs: http://developer.android.com/guide/topics/ui/dialogs.html
Toasts: http://developer.android.com/guide/topics/ui/notifiers/toasts.html
But if you want to access or modify a control in your actual activity, then use Runnable class, and context.runOnUiThread() method if you work inside AsyncTask. The real problem is that you can't change UI in a AsyncTask using declaration of the controls. You need to throw a Runnable process to communicate with activity!!. For example:
context.runOnUiThread(new Runnable() {
public void run() {
//Declaration of variables
TextView MyTextView = (TextView) context.findViewById(R.id.txtvMyControl);
MyTextView.setText("My title");
}
}
If I can helps you say me, good luck!

OnPostExecute() function is not executing in AsyncTask

Hello i'm developing an application that performs database queries on a remote server. so far i've successfully interacted with the PHP script on the server side and my code is sending string values without a problem, but i'm not sure how to check if i'm recieving back the values i'm sending back through my PHP script, as my OnPostExecute function is not responding.
Please Help.
Here's the code that's facing problem :
public void submit_data(View V){
try{
new DoSocketProgramming().execute("10.0.2.2");
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "problem here", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getApplicationContext(), "created till here", Toast.LENGTH_SHORT).show();
}
public class DoSocketProgramming extends AsyncTask<String, Void, String>
{
String sendsentence=" this message is sent\n";
String recvsentence=null;
protected void onPreExecute()
{
Toast.makeText(getApplicationContext(), "this is preExecute", Toast.LENGTH_LONG).show();
}
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
Socket con=new Socket(addr,1678);
DataInputStream dis=new DataInputStream(con.getInputStream());
DataOutputStream dos=new DataOutputStream(con.getOutputStream());
dos.writeUTF(sendsentence);
recvsentence=dis.readUTF();
}catch(Exception e){
e.printStackTrace();
}
return recvsentence;
}
protected void onPostExecute(String result) {
try{
Toast.makeText(getApplicationContext(), "this is post execute"+ recvsentence, Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
}
}
It probably is, actually. The problem is that you're using getApplicationContext() as the Context and this is almost always wrong, you shouldn't use this unless you know exactly what you're doing.
When creating an AsyncTask, the constructor receives the Context of the Activity that invoked it, you should store this Context in your AsyncTask class and use it in your onPostExecute() method.
This would be an example:
public class MyAsyncTask extends AsyncTask {
Context context;
private MyAsyncTask(Context context) { this.context = context; }
#Override
protected void onPostExecute(...) {
super.onPostExecute(result);
Toast.makeText(context, "this is post execute"+ recvsentence, Toast.LENGTH_LONG).show();
}
}

android asynctask NetworkOnMainThreadException

I'm trying to write a server/client app in android using sockets and i handle the client socket in AsyncTask (server is not android, just ordinary java).I get the exception when I'm trying to read from the server.I found out that when I delete android:targetSdkVersion="16" from android manifest the exception goes away and I can read from the server.
I don't understand why is that? could anyone help me clarify this? I also have problems understanding how the asynctask method doInBackground and my own methods relate. Does conhandler.execute() run doInBackground() and then just waits until I call the other methods? thanks for help.
public class ConnectionHandler extends AsyncTask<Void, Void, Void>{
public static String serverip = "10.0.2.2";
public static int serverport = 5000;
Socket s;
PrintWriter out;
BufferedReader in;
protected Void doInBackground(Void... params) {
try {
s = new Socket(serverip, serverport);
Log.i("AsyncTank", "doInBackgoung: Created Socket");
}...
if (s.isConnected()) {
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream(), true);
Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");
} ....
}
public void writeToStream(String message) {
try {
if (s.isConnected()){
out.println(message);
} else {
Log.i("AsynkTask", "writeToStream : Cannot write to stream, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "writeToStream : Writing failed");
}
}
public String readFromStream() {
try {
if (s.isConnected()) {
Log.i("AsynkTask", "readFromStream : Reading message");
String ret=in.readLine();
Log.i("AsynkTask", "readFromStream : read "+ret);
return ret;
} else {
Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
}
} catch (Exception e) {
Log.i("AsynkTask", "readFromStream : Reading failed"+e.getClass());
}
return null;
}
}
this is my main activity
public class MainActivity extends Activity {
private EditText view_email;
private EditText view_password;
TextView result;
ConnectionHandler conhandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conhandler = new ConnectionHandler();
conhandler.execute();
}
public void register(View view) {
view_email= (EditText) findViewById(R.id.email);
view_password = (EditText) findViewById(R.id.password);
String email=view_email.getText().toString();
String password=view_password.getText().toString();
conhandler.writeToStream("register");
conhandler.writeToStream(email);
conhandler.writeToStream(password);
String res=conhandler.readFromStream(); //here's the exception
result=(TextView) findViewById(R.id.result);
result.setText(res);
}
}
There are two Solution of this Problem.
1) Don't write network call in Main UIThread, Use Async Task for that.
2) Write below code into your MainActivity file after setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
And below import statement into your java file.
import android.os.StrictMode;
android.os.NetworkOnMainThreadException
this eror comes With HoneyComb(3.0 or Later).
you can not perform a networking operation on its main thread as documentation says. to getting ride of this you must use handler or asynctask. AFAIK There is no another way to do it.
you can See this for More Details WHY ICS Crashes your App
Try Using Below Code Snippet
new Thread(){
public void run(){
//do your Code Here
}
}.start();

How to change activity after an async task is completed?

The problem has been solved, thank you for the help.
Essentially, I have my onCreate() class that sets an OnClick listener on a button. Once I click it, an async task is created and executed.
This async task gets a response from a server and then sends the info to onPostExecute().
From here, if the info is valid or not, I want to toast the result and switch activities accordingly.
As of now, I have tried to do it from the onPostExecute() and it does not work. Somehow, I must get back to my main thread and do it there. How do I do this?
Here is the code:
public class Class1 extends Activity {
JSONObject jObj = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
Button submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
(new CallAPI(restfulCall)).execute();
}
});
}
public class CallAPI extends AsyncTask<Void,Void,String>
{
private String restfulCall = "";
public CallAPI(String command)
{
restfulCall = command;
}
protected String doInBackground(Void... urls)
{
return API.getData(restfulCall);
}
protected void onPostExecute(String result) {
try {
jObj = new JSONObject(result);
} catch (JSONException e1) {
e1.printStackTrace();
}
try {
if(jObj.get("status").toString().equals("success"))
{
Toast.makeText(getBaseContext(),"Registration Succesful",Toast.LENGTH_LONG).show();
Intent intent = new Intent(Class1.this,Success.class);
Class1.this.startActivity(intent);
}
else
{
Toast.makeText(getBaseContext(),jObj.get("error").toString(),Toast.LENGTH_LONG).show();
Intent intent = new Intent(Class1.this,Error.class);
Class1.this.startActivity(intent);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
use equals for comparing Strings intead of == as :
if(jObj.get("status").toString().equals("success")
instead of
if(jObj.get("status").toString() == "success")

One Android chat app unable to receive messages ( aSamck + Openfire )

I have implemented a chat application using aSmack. I used the openfire server as the chat server. All of these applications are running in the same machine. But when I try to send messages between two emulators only one emulator successfully receives messages. Other client won't receive any messages. But from both emulators I was able to send messages to pigin(IM clinet). Also if I use gmail.com as the chat server everything works just fine.
User names used to login
jayamal
suchith
(openfire indicates users are online )
names used to send messages
jayamal#elearn (elearn is the domain i created in my machine using openfire)
suchith#elearn
( but in openfire archives shows one name as jayamal#elearn/Smack, tried sending message to that name but it also unsuccessful )
Please help to rectify this problem. Your help is really appreciated.
public class ASmackChatTestActivity extends Activity {
public int state = 0;
private static final String TAG = "HelloFormStuffActivity";
XMPPConnection xmpp ;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnLogin = (Button) findViewById(id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText txtUserName = (EditText) findViewById(id.txtUserName);
EditText txtPass = (EditText) findViewById(id.txtPass);
String userName = txtUserName.getText().toString();
String password = txtPass.getText().toString();
new login().execute(userName,password);
}
});
Button btnSend = (Button) findViewById(id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText txtMessage = (EditText) findViewById(id.txtMessage);
EditText txtTo = (EditText) findViewById(id.txtTo);
String message = txtMessage.getText().toString();
String to = txtTo.getText().toString();
new sendMessage().execute(to,message);
}
});
Button btnStop = (Button) findViewById(id.btnStopServices);
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText txtTo = (EditText) findViewById(id.txtTo);
String to = txtTo.getText().toString();
new recieveMessages().execute(to);
}
});
}
class login extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String userName = params[0];
String password = params[1];
//XMPPConnection xmpp = new XMPPConnection("jabber.iitsp.com");
xmpp = new XMPPConnection("10.0.2.2");
try {
xmpp.connect();
// for other jabber accounts, truncate after the #
//xmpp.login("username", "password");
// for gtalk / gmail, include the #
xmpp.login(userName, password);
Log.v(TAG,"Logged in");
} catch (XMPPException e) {
Log.v(TAG, "Failed to connect to " + xmpp.getHost());
e.printStackTrace();
}
return null;
}
}
class sendMessage extends AsyncTask<String, Void, String>{
//String msg;
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String to = params[0];
String message = params[1];
ChatManager chatmanager = xmpp.getChatManager();
Chat newChat = chatmanager.createChat(to, new MessageListener() {
// THIS CODE NEVER GETS CALLED FOR SOME REASON
public void processMessage(Chat chat, Message message) {
try {
// msg = message.getBody();
Log.v(TAG, "Got:" + message.getBody());
chat.sendMessage(message.getBody());
} catch (XMPPException e) {
Log.v(TAG, "Couldn't respond:" + e);
}
Log.v(TAG, message.toString());
}
});
// Send something to friend#gmail.com
try {
newChat.sendMessage(message);
Log.v(TAG, "sent:" + message);
} catch (XMPPException e) {
Log.v(TAG, "couldn't send:" + e.toString());
}
return null;
}
#Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
//Toast.makeText(getBaseContext(),"Message Recieved : " + msg, Toast.LENGTH_LONG);
super.onPostExecute(result);
}
}
class recieveMessages extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String to = params[0];
// Accept only messages from friend#gmail.com
PacketFilter filter
= new AndFilter(new PacketTypeFilter(Message.class),
new FromContainsFilter(to));
// Collect these messages
PacketCollector collector = xmpp.createPacketCollector(filter);
while(true) {
Packet packet = collector.nextResult();
if (packet instanceof Message) {
Message msg = (Message) packet;
// Process message
Log.v(TAG, "Got message: " + msg.getBody());
}
}
//return null;
}
}
}
Sorry this is a bit late.
The one user you can send to the IM client (pidgin) can you send back to your emulator. I.e. can you receive in either emulator?
Message receiving is event based so you don't need to use a button click to set it off.
Check out this great example. By Davanum Srinivas
I've modified it for my use quite extensively but the base code is still very useful.
http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/ also look at the original article.

Categories

Resources