I don't know where is sendinfo()
called? But I think in your main Activity you call that:
new SendData().sendInfo();
:(
Please put sendInfo to doInBackground
And in your Activity: new SendData().execute();
Question
I am trying to send some data to my server which is publicly available on the internet. The app gets started as a service after boot is completed (permission is added), how ever the app crashes. I have narrowed the code down. It seems like it crashes when it is trying to send data to the server.
Below is my code. I really appreciate if you guys can help me on this.
package com.example.bootstart;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.os.AsyncTask;
import android.util.Log;
class SendData extends AsyncTask<Void, Void, Boolean> {
public void sendinfo(){
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
// Creating HTTP Post
HttpPost httpPost = new HttpPost(
"http://www.mydomain.com/controller_name/funtion_name");
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(4);
nameValuePair.add(new BasicNameValuePair("imei", "12345"));
nameValuePair.add(new BasicNameValuePair("welawa", "12315646545"));
nameValuePair.add(new BasicNameValuePair("lat", "123.25"));
nameValuePair.add(new BasicNameValuePair("long", "52.22323"));
// Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
I am a php developer, trying to write a simple app that logs GPS cordinates.
I can't check the console because some thing has made the emulator working. I always get the emulator-arm.exe has stopped working bla bla bla. So I use my phone and restart it everytime when I test. So unfortunately I do not have access to a log either.
Any help is greatly appreciated.
Solution
I don't know where is sendinfo()
called? But I think in your main Activity you call that:
new SendData().sendInfo();
:(
Please put sendInfo to doInBackground
And in your Activity: new SendData().execute();