Every HTTP request throws failed to connect to Server wherever App goes in sleep or minimize mode. This only happens in Android Pie(9.0).
Please check my code below,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AsyncReadConfig asyncReadConfig = new AsyncReadConfig();
asyncReadConfig.execute();
}
});
}
class AsyncReadConfig extends AsyncTask<Integer, Integer, Object> {
#Override
protected Object doInBackground(Integer... integers) {
try {
return callGetURLService(CONFIGURATION_URL);
} catch (Exception e) {
return e;
}
}
}
private Object callGetURLService(String path) {
try {
URL url = new URL(path);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String text;
StringBuilder configParams = new StringBuilder();
while ((text = in.readLine()) != null) {
configParams.append(text);
}
JSONObject jsonObject = new JSONObject(configParams.toString());
//Doing my stuff
return null;
} catch (Exception e) {
e.printStackTrace();
}
}
In my manifeast.xml,
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:networkSecurityConfig="#xml/network_security_config"
.
.
In network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">MY_DOMAIN</domain>
</domain-config>
</network-security-config>
I'm just reading text file from server in sleep mode and I'm getting error,
java.net.ConnectException: Failed to connect to server
And as I'm switching my App from minimize mode to active mode, everything is working fine.
When your app is in the background you cant make network calls unless you are using a Foreground service to make the network call
On virtual device is working, on real device not. Checked all ip, many ports, but nothing.
Android 7.1.1 Lenovopad TB-X304F
In manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Code:
public class MainActivity extends AppCompatActivity {
EditText e1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText)findViewById(R.id.editText);
}
public void send_text(View v) {
String message = e1.getText().toString();
myTask mt = new myTask();
mt.execute(message);
Toast.makeText(getApplicationContext(),"Data sent: "+e1.getText().toString(), Toast.LENGTH_LONG).show();
}
class myTask extends AsyncTask<String, Void, Void>{
Socket s;
PrintWriter printWriter;
#Override
protected Void doInBackground(String... params){
try {
String message = params[0];
s = new Socket("192.168.1.5", 6001);
printWriter = new PrintWriter(s.getOutputStream());
printWriter.write(message);
printWriter.flush();
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Error:
W/System.err: java.net.ConnectException: Connection timed out
The mistake was that I did not know that I had to disable the firewall and open the port. I opened the port and it all worked.
I am working on a login in application. I am trying to display progressDialog in my BackgroundWorker class which extends Async task. But as soon as I click on Login button, the progress dialog is shown just for a few seconds. Is there any possibility that signing process will last longer?
Snippets of the code
`public class BackgrroundWorker extends AsyncTask<String,Void,String>{
Context context;
AlertDialog alertDialog;
boolean correct;
public BackgrroundWorker(Context context){
this.context = context;
}
#Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "random";
String register_url = "random";
if(type.equals("login")){
try {
String username = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username","UTF-8") + "=" +URLEncoder.encode(username,"UTF-8")+ "&"
+URLEncoder.encode("password","UTF-8") + "=" +URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result = "";
String line = "";
correct = false;
while((line = bufferedReader.readLine()) != null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
System.out.println(result);
if (result.equalsIgnoreCase("login success"))
correct=true;
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Login",
"Please wait for a while.", true);
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login status");
}
#Override
protected void onPostExecute(String result) {
if(!result.equals("login success")) {
alertDialog.setMessage(result);
alertDialog.show();
}
progressDialog.dismiss();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
`
progressDialog.dismiss();
This statement causing dialog to disappear and you have set it without any condition inside
onPostExecute
If you want to show dialog for a long time place it inside Handler or CountDownTimer. You can also make it user interaction based like button click.
For example, if you want to dismiss dialog for 5 sec:
#Override
protected void onPostExecute(String result) {
if(!result.equals("login success")) {
alertDialog.setMessage(result);
alertDialog.show();
}
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
progressDialog.dismiss();
}
}.start();
}
Is there any possibility that signing process will last longer?
(signing = signin)
There sure is. Imagine the device is being heavily throttled, the server is taking along time to respond, or stuck on a really really slow network (ex: 1g data network, or my local starbucks wifi that 100s for people use at the same time)
You can test this by loading your application on the android emulator and using the emulator console commands for netspeed and netdelay
The basics steps are something like :
telnet localhost 5554
network speed 2 1
The first number ("2"). is the upload speed (traffic from the device) in kilobytes per second
The second number ("1"), is the download speed (traffic to the device) in kilobytes per second
network netdelay 10000
Above, we set a 10 second delay/latency of the network
then on the device, perform the login
Full documentation to accomplish this is here and I recommend checking them out. Lots of great info : https://developer.android.com/studio/run/emulator-console.html
I also found this answer to be helpful:
https://stackoverflow.com/a/6236379/794088
I am trying to implement a telnet client in android that is going to continuously read data from a telnet server. I have verified that the server is sending data continuously by connecting to it from a telnet app from play store.
Now im trying to create an app to do the same and display the data on the screen using apache telnetclient. I have successfully connected to the telnet server, and I am able to read the correct lines for 5-10 seconds. The code is posted below.
What works:
Connecting to server.
Reading some lines for 5-10 seconds before the stream stops.
The problem:
The reading seems to stop after around 5-10 seconds, and hangs on readLine() with no new data coming in. Is this a problem with the code? Any help would be appreciated!
UPDATE:
I am trying to run this on an Android 4.2.2 device. I tried it on a newer android 7 device just now, and the code works as intended, I get continuous readings. Is there something in the android version that can be causing this?
--
This is the TestTelnetClient.class
public class TestTelnetClient extends Activity {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String server = "192.168.36.105";
TextView outputView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputView = (TextView)findViewById(R.id.output);
}
public void sendTelnet(View view) {
TelnetRead telnetRead = new TelnetRead();
telnetRead.execute();
}
private class TelnetRead extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... params) {
try {
// Connect to the specified server
telnet.connect(server, 8110);
// Get input and output stream references
in = telnet.getInputStream();
//out = new PrintStream(telnet.getOutputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String aad = r.readLine();
while (true) {
publishProgress(aad);
aad = r.readLine();
}
//telnet.disconnect();
//finish();
//return aad;
//return "exit";
}
catch (Exception e) {
e.printStackTrace();
return e.toString();
}
}
#Override
protected void onPostExecute(String result) {
try {
outputView.setText(result);
Thread.sleep(10);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
#Override
protected void onPreExecute() {
}
//#Override
protected void onProgressUpdate(String... result) {
outputView.setText(result[0]);
}
}
This is the android manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".TestTelnetClient"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I am developing one application and in that application I need to implement push notifications. Can anyone suggest how I should go about implementing push notifications? If I could be provided with a good tutorial, that would be great!
Thanks.
// Call when your application start
public void StartRegistrationNotification()
{
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "....#gmail.com");
this.startService(registrationIntent);
}
// change in Manifest File
<receiver android:name="com.ReceiverC2DM"
android:permission="com.google.android.c2dm.permission.SEND">
<!-- Receive the actual message -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="yourpackagename" />
</intent-filter>
<!-- Receive the registration id -->
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="yourpackagename" />
</intent-filter>
</receiver>
<permission android:name="yourpackagename.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="yourpackagename.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive message -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
// ReceiverC2DM.java .....
public class ReceiverC2DM extends BroadcastReceiver {
private static String KEY = "c2dmPref";
private static String REGISTRATION_KEY = "registrationKey";
private Context context;
// wakelock
private static final String WAKELOCK_KEY = "C2DM_FAX";
private static PowerManager.WakeLock mWakeLock;
#Override
public void onReceive(Context context, Intent intent) {
this.context = context;
runIntentInService(context, intent);
if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) {
handleRegistration(context, intent);
} else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) {
handleMessage(context, intent);
}
}
private void handleRegistration(Context context, Intent intent) {
String registration = intent.getStringExtra("registration_id");
Log.e("registration :","registration :"+registration);
if (intent.getStringExtra("error") != null) {
// Registration failed, should try again later.
Log.d("c2dm", "registration failed");
String error = intent.getStringExtra("error");
if(error == "SERVICE_NOT_AVAILABLE"){
Log.d("c2dm", "SERVICE_NOT_AVAILABLE");
}else if(error == "ACCOUNT_MISSING"){
Log.d("c2dm", "ACCOUNT_MISSING");
}else if(error == "AUTHENTICATION_FAILED"){
Log.d("c2dm", "AUTHENTICATION_FAILED");
}else if(error == "TOO_MANY_REGISTRATIONS"){
Log.d("c2dm", "TOO_MANY_REGISTRATIONS");
}else if(error == "INVALID_SENDER"){
Log.d("c2dm", "INVALID_SENDER");
}else if(error == "PHONE_REGISTRATION_ERROR"){
Log.d("c2dm", "PHONE_REGISTRATION_ERROR");
}
} else if (intent.getStringExtra("unregistered") != null) {
// unregistration done, new messages from the authorized sender will be rejected
Log.d("c2dm", "unregistered");
} else if (registration != null) {
Log.d("c2dm", registration);
Editor editor =
context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
editor.putString(REGISTRATION_KEY, registration);
editor.commit();
// Send the registration ID to the 3rd party site that is sending the messages.
// This should be done in a separate thread.
// When done, remember that all registration is done.
}
}
private void handleMessage(Context context, Intent intent)
{
String message = intent.getExtras().getString("payload");
String key = intent.getExtras().getString("collapse_key");
Log.e("","accountName : " +accountName);
Log.e("","message : " +message);
Intent startActivity = new Intent();
startActivity.setClass(context, NotificationAlert.class);
startActivity.setAction(NotificationAlert.class.getName());
startActivity.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity.putExtra("Title", "Hello");
startActivity.putExtra("Message", message);
context.startActivity(startActivity);
//Do whatever you want with the message
}
static void runIntentInService(Context context, Intent intent) {
if (mWakeLock == null) {
// This is called from BroadcastReceiver, there is no init.
PowerManager pm =
(PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
WAKELOCK_KEY);
}
mWakeLock.acquire();
}
// Server Side ... New Test App
ServerSimulator.java
private SharedPreferences prefManager;
private final static String AUTH = "authentication";
private static final String UPDATE_CLIENT_AUTH = "Update-Client-Auth";
public static final String PARAM_REGISTRATION_ID = "registration_id";
public static final String PARAM_DELAY_WHILE_IDLE = "delay_while_idle";
public static final String PARAM_COLLAPSE_KEY = "collapse_key";
private static final String UTF8 = "UTF-8";
// Registration is currently hardcoded
private final static String YOUR_REGISTRATION_STRING = "put registration key";
private SharedPreferences prefs;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefManager = PreferenceManager.getDefaultSharedPreferences(this);
}
public void getAuthentification(View view) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://www.google.com/accounts/ClientLogin");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Email", "....#gmail.com"));
nameValuePairs.add(new BasicNameValuePair("Passwd","....."));
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("source",
"Google-cURL-Example"));
nameValuePairs.add(new BasicNameValuePair("service", "ac2dm"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
if (line.startsWith("Auth=")) {
Editor edit = prefManager.edit();
edit.putString(AUTH, line.substring(5));
edit.commit();
String s = prefManager.getString(AUTH, "n/a");
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void showAuthentification(View view) {
String s = prefManager.getString(AUTH, "n/a");
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
public void sendMessage(View view) {
try {
Log.e("Tag", "Started");
String auth_key = prefManager.getString(AUTH, "n/a");
// Send a sync message to this Android device.
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
.append(YOUR_REGISTRATION_STRING);
// if (delayWhileIdle) {
// postDataBuilder.append("&").append(PARAM_DELAY_WHILE_IDLE)
// .append("=1");
// }
postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
.append("0");
postDataBuilder.append("&").append("data.payload").append("=")
.append(URLEncoder.encode("Fax Sent ... Test Push Notification ....", UTF8));
Log.e("postDataBuilder ","postDataBuilder :" + postDataBuilder.toString());
byte[] postData = postDataBuilder.toString().getBytes(UTF8);
// Hit the dm URL.
URL url = new URL("https://android.clients.google.com/c2dm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length",
Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth="
+ auth_key);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
Log.e("Tag", String.valueOf(responseCode));
// Validate the response code
if (responseCode == 401 || responseCode == 403) {
// The token is too old - return false to retry later, will
// fetch the token
// from DB. This happens if the password is changed or token
// expires. Either admin
// is updating the token, or Update-Client-Auth was received by
// another server,
// and next retry will get the good one from database.
Log.e("C2DM", "Unauthorized - need token");
}
// Check for updated token header
String updatedAuthToken = conn.getHeaderField(UPDATE_CLIENT_AUTH);
if (updatedAuthToken != null && !auth_key.equals(updatedAuthToken)) {
Log.i("C2DM",
"Got updated auth token from datamessaging servers: "
+ updatedAuthToken);
Editor edit = prefManager.edit();
edit.putString(AUTH, updatedAuthToken);
}
String responseLine = new BufferedReader(new InputStreamReader(
conn.getInputStream())).readLine();
// NOTE: You *MUST* use exponential backoff if you receive a 503
// response code.
// Since App Engine's task queue mechanism automatically does this
// for tasks that
// return non-success error codes, this is not explicitly
// implemented here.
// If we weren't using App Engine, we'd need to manually implement
// this.
if (responseLine == null || responseLine.equals("")) {
Log.i("C2DM", "Got " + responseCode
+ " response from Google AC2DM endpoint.");
throw new IOException(
"Got empty response from Google AC2DM endpoint.");
}
String[] responseParts = responseLine.split("=", 2);
if (responseParts.length != 2) {
Log.e("C2DM", "Invalid message from google: " + responseCode
+ " " + responseLine);
throw new IOException("Invalid response from Google "
+ responseCode + " " + responseLine);
}
if (responseParts[0].equals("id")) {
Log.i("Tag", "Successfully sent data message to device: "
+ responseLine);
}
if (responseParts[0].equals("Error")) {
String err = responseParts[1];
Log.w("C2DM",
"Got error response from Google datamessaging endpoint: "
+ err);
// No retry.
throw new IOException(err);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
C2DM is deprecated and has been replaced by Google Cloud Messaging for Android (GCM).
Google's statement:
Important: C2DM has been officially deprecated as of June 26, 2012.
This means that C2DM has stopped accepting new users and quota
requests. No new features will be added to C2DM. However, apps using
C2DM will continue to work. Existing C2DM developers are encouraged
to migrate to the new version of C2DM, called Google Cloud Messaging
for Android (GCM). See the C2DM-to-GCM Migration document for more
information. Developers must use GCM for new development.
There is a demo app tutorial for GCM and also for migration from C2DM.