android get restful api always timeout - java

I use httpclient(apache) to get a restful API when developing an Android app,just like
HttpGet httpget = new HttpGet("http://9.123.151.73:4414/apiv1/data");
HttpResponse response = httpclient.execute(httpget);
and the xml,I have set the permission:
`<uses-permission android:name="android.permission.INTERNET" />`
but it always timeout.
Then I try these cases:
Open the url in browser,it works
New a simple java project with the same code, it works
Replace the url with other rest API ,such as:
http://api.search.yahoo.co/NewsSearchService/V1/, works
So, could you give me any advices for this problem,I wonder why the rest API can not be used in Android app,what is the mistake?

I beleive it is due to the permission not set to make network connections. Here is what you need to add to your AndroidMainfest.xml file to allow access to the internet:
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
Here is the permission description:
INTERNET Allows applications to open network sockets.
You can check more about the permission from the link:
Here is how you can set the permission if you are using eclipse:
If you are using the Eclipse ADT plugin for your development, open
"AndroidManifest.xml" in the Android Manifest Editor (should be the
default action for opening androidmanifest.xml from the project files
list), select the "Permissions" tab along the bottom of the editor
(Manifest - Application - Permissions - Instrumentation -
AndroidManifest.xml), then "Add..." a "Uses Permission" and select the
desired permission from the dropdown on the right, or just copy paste
in the necessary one (such as the "android.permission.INTERNET"
permission you required
http://developer.android.com/reference/android/Manifest.permission.html

Related

Android App doesn't have internet connection - No Permissions required

Well, I have built an app to store values on a remote database. It works!! I didn't use an emulator for testing instead I used my own phone. Now the problem is that on my phone, it works perfectly...no issues but when I installed it on another phone, the app doesn't connect to the internet.
I have included internet permission in android manifest.xml.
I have tried these but didn't work:
Building apk and installing in the new phone.
Compiling directly to the new phone.
Sending the apk from old phone to new phone via shareit.
Creating a signed apk and installing.
Checked via wifi and mobile data (NOTE: BOTH WIFI AND MOBILE DATA WORKS IN OLD PHONE).
Checked android compatibility: supports up to android 10.
*I added error messages for try...catch blocks in form of toasts for the user to know what's the issue. and the catch exception for no internet returns connection problem. I'm getting that error message.
As I researched, I got to know that internet permission is categorized as normal permission which is not prompted to the user upon installation.
I built a second dummy app: Same issue with it... compiled directly to new phone but didn't work...it works in old phone...
Old phone: Samsung J7 Prime with Android 8.1
New Phone: Samsung J7 Pro with Android 9
Any idea or suggestion will be gladly helpful... Thank you!
in android 9 and above you have to set network Security Config
first of all in res package create xml package and in xml package create new xml resource file with network_security_config name
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
then in manifest in
android:networkSecurityConfig="#xml/network_security_config"
<uses-permission android:name="android.permission.INTERNET" />
Use debug APK instead of signed APK
please add permission on Manifest to give a permission of permission.INTERNET

How to get "Device ID" from Android device under software update settings

Does anyone know of a way to retrieve the Device ID found under Settings -> About tablet -> System updates -> Software update settings under the Device information header in Android versions 4.x or 5.x?
Agreed with #G.hakim , but additional to that, this API requires android.permission.READ_PHONE_STATE permission.So you need to declare the permission in AndroidManifest.xml if you want to use this api:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
If i'm not wrong you are looking for your mobile phone's device id which you can get using these line of code
TelephonyManager tm = (TelephonyManager)context.GetSystemService(Context.TelephonyService);
deviceId = tm.DeviceId;
Also requires the following permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Also after Android Marshmallow Or API 23 you need to ask for permissions on the fly the code reference for that can be found here:
https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

Last.fm API call from Android application

I'm trying to make last.fm API call from android application using this package: http://www.u-mass.de/lastfm
From simple java command line program, it works, but not in android application. Using windows and Eclipse
Code itself is very easy:
Artist artist = Artist.getInfo("Depeche Mode", "my_key");
I have set internet permission in android manifest
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
After call I end up here, where it prints "caching failed."
http://code.google.com/p/lastfm-java/source/browse/trunk/src/de/umass/lastfm/Caller.java?spec=svn173&r=173
response code there is -1
int responseCode = urlConnection.getResponseCode();
Any ideas what I'm doing wrong?
I was facing the same problem.
Its a reported bug.
Solution I found was this :
Just add this line somewhere in your code that will get run prior to calling any of the api methods:
Caller.getInstance().setCache(null);
Note: this will disable Caching..
for more info check this link

jdom not fetching anything from url [duplicate]

I want to parse an rss feed from an android application. Everything related to parsing the RSS feed itself is done (using SAX), however I get an exception regarding the name resolution of the feed's url.
This is the line causing the exception:
feedUrl = "http://blog.jonathanbenoudiz.com/feed/"
feedUrl.openConnection().getInputStream();
java.lang.RuntimeException:
java.net.UnknownHostException:
So I started investigating my /etc/resolv.conf file and set the nameserver to the dns server of my ISP. Pinging blog.jonathanbenoudiz.com works, but http://blog.jonathanbenoudiz.com and blog.jonathanbenoudiz.com/feed don't work (unknown host).
How am I actually supposed to do this?
Thanks!
Add the INTERNET permission to your manifest file.
You have to add this line:
<uses-permission android:name="android.permission.INTERNET" />
outside the application tag in your AndroidManifest.xml

Java.lang.SecurityException:SECURE PERMISSION in android?

Iam getting this error on my log
07-06 06:22:07.419: ERROR/AndroidRuntime(2618): java.lang.SecurityException: SECURE PERMISSION: Neither user 10070 nor current process has android.permission.WRITE_SECURE_SETTINGS.
I used like this
in activity file
private static final String SECURE_SETTINGS = android.Manifest.permission.WRITE_SECURE_SETTINGS;
mContext.enforceCallingOrSelfPermission(SECURE_SETTINGS,
"BLUETOOTH_ADMIN permission");
in manifest file
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
Why does it give error?
The WRITE_SECURE_SETTINGS permission is not available to apps which aren't part of the firmware because secure settings are designed to be secured against modification by third party apps.
You can only read this settings if your phone doesn't have root access. To enable bluetooth you can use this references http://developer.android.com/guide/topics/wireless/bluetooth.html.
However, this is above sdk5... I'm not really sure if it is a good idea to use bluetooth with sdk3. I also wanted to do an app which uses bluetooth and I had to switch my target level to above 2.0.1 because bluetooth below this sdk is not supported very well.

Categories

Resources