Unable to resolve host, no address associated with hostname - android

I just got started with Android Studio and made a small application. My problem is that i cannot connect to a website. I've added volley to my project to handle the responses. I searched other similar questions but all the responses suggested to add a permission to the manifest file(which i already have it).
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When the request is being made i get back an error: java.net.UnknownHostException: Unable to resolve host "url goes here": No address associated with hostname
The url works, i tried accessing it from my browser.
What can i do?
EDIT
StringRequest request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override public void onResponse(String response)
{
Log.e("resp", response);
Toast.makeText(context.getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override public void onErrorResponse(VolleyError error)
{
Log.d("error", error.getMessage());
}
});
VolleyCore.getInstance(context).addToRequestQueue(request);

Related

Android app ( in emulator ) does not connect to local rest API through http

Android avd emulator to local api in docker. I can access to it through postman or any web browser.
#Override
public Map<?, ?> getData() {
String url = "http://10.0.2.2:8000/rest/habits/";
RequestQueue requestQueue = Volley.newRequestQueue(this.context);
StringRequest jsonObjectRequest = new StringRequest(
Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String a = "1";
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
String a = "";
}
});
requestQueue.add(jsonObjectRequest);
return null;
}
}
The only error I got is "com.android.volley.ClientError", what the heck is wrong?
Ty in advance.
edit:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.habits_builder_android">
<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:usesCleartextTraffic="true"
android:theme="#style/Theme.Habits_builder_android">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/Theme.Habits_builder_android.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And it is Android API 28.
And the only thing in the traceback that could useful and I did not mention is: E/Volley: [549] BasicNetwork.performRequest: Unexpected response code 400 for http://10.0.2.2:8000/rest/habits/.
I know, bad request, but it is a simple get and I've copy/pasted the headers in postman and it works.

Volley jsonArray Request not working

I am making jsonArray request using volley but onError() gets executed all the time though I have added dependency, internet permission.
Here is my request code
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, "http://api.aladhan.com/v1/timingsByCity?city=Dubai&country=United%20Arab%20Emirates", null, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
try {
JSONObject obj = (JSONObject) response.get(0);
Toast.makeText(City_Timing.this, obj.getString("Fajr"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(City_Timing.this, "Error", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonArrayRequest);
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="anandroid.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=".MainActivity">
</activity>
<activity android:name=".Search_CIty"
android:label="SEARCH CITY"
android:parentActivityName=".MainActivity"/>
<activity android:name=".City_Timing"
android:label="TIMING"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".History"
android:label="HISTORY"
android:parentActivityName=".MainActivity"/>
<activity android:name=".AboutUS"
android:label="ABOUT US"
android:parentActivityName=".MainActivity"/>
</application>
Only toast of onError is displaying. I have tried to change the request type to string, JsonObject request but nothing working, only on error is executed all the time.
You have got a typo error in internet permission, change
<uses-permission android:name="anandroid.permission.INTERNET"/>
to
<uses-permission android:name="android.permission.INTERNET" />
it should be android not anandroid
please check your menifest:
there is a spelling mistake in anandroid
<uses-permission android:name="anandroid.permission.INTERNET"/>
change to
<uses-permission android:name="android.permission.INTERNET"/>
it works fine try this and let me know
Just change this,
<uses-permission android:name="anandroid.permission.INTERNET"/>
To,
<uses-permission android:name="android.permission.INTERNET" />
You just got a typo error, nothing else.
Try This
<uses-permission android:name="anandroid.permission.INTERNET"/>
instead of this
<uses-permission android:name="android.permission.INTERNET" />

No Client Token found, please set the Client Token

I am trying to create the login with facebook in android and I am running into this issue. I am not sure what else I am failing to see or set.
I tried setting app toke / client secret but it keeps failing.
Below is my code:
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.test.app">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FacebookLoginActivity">
<intent-filter>
<action android:name="android.intent.action.FACEBOOK_LOGIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
tools:replace="android:theme" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/facebook_app_id"/>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
FacebookLoginActivity
package com.test.app;
public class FacebookLoginActivity extends AppCompatActivity {
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.facebook_login);
info = (TextView) findViewById(R.id.info);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}
#Override
public void onCancel() {
info.setText("Login attempt canceled.");
}
#Override
public void onError(FacebookException error) {
info.setText("Login attempt failed.");
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
ERROR
Caused by: java.lang.IllegalStateException: No Client Token found, please set the Client Token.
at com.facebook.internal.Validate.hasClientToken(Validate.java:162)
at com.facebook.login.DeviceAuthDialog.startLogin(DeviceAuthDialog.java:178)
at com.facebook.login.DeviceAuthMethodHandler.showDialog(DeviceAuthMethodHandler.java:51)
at com.facebook.login.DeviceAuthMethodHandler.tryAuthorize(DeviceAuthMethodHandler.java:42)
at com.facebook.login.LoginClient.tryCurrentHandler(LoginClient.java:254)
at com.facebook.login.LoginClient.tryNextHandler(LoginClient.java:216)
at com.facebook.login.LoginClient.authorize(LoginClient.java:121)
at com.facebook.login.LoginClient.startOrContinueAuth(LoginClient.java:102)
at com.facebook.login.LoginFragment.onResume(LoginFragment.java:153)
at android.support.v4.app.Fragment.performResume(Fragment.java:2230)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1343)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1523)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1585)
at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2842)
at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:223)
at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:509)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:498)
at android.app.Activity.performResume(Activity.java:6807)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3406)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3469) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2732) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
You'll need to add your app client token to your AndroidManifest like this:
<meta-data
android:name="com.facebook.sdk.ClientToken"
android:value="#string/facebook_client_token"/>
You can find your app client token in facebook dashboard -> settings -> advanced (in the security section)
in the login button cross check
<com.facebook.login.widget.LoginButton -> this is right
<com.facebook.login.widget.DeviceLoginButton -> this is wrong

my local Parse server is not working

I am trying to use Parse on my Windows PC with Android Studio.
I followed some tutorials to install MongoDB, Parse server and Parse dashboard, and all went smooth.
When I open Android Studio, the Parse server initializes, but it doesn't save the data and I don't what went wrong
Here is my manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.starter" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name=".StarterApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="kooora.com100plus" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="kooora.com100plusMasterKey" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I changed the key and the client ID as the same ones when I set my Parse Server
And here is the code for the start application Activity:
public class StarterApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
// Add your initialization code here
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
.applicationId("kooora.com100plus")
.clientKey("kooora.com100plusMasterKey")
.server("http://localhost:1337/parse/")
.build()
);
ParseObject gameScore = new ParseObject("GameScore");
gameScore.put("score", 1337);
gameScore.put("playerName", "Sean Plott");
gameScore.put("cheatMode", false);
gameScore.saveInBackground(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
Log.i("Parse", "Save Succeeded");
} else {
Log.i("Parse", e.getMessage());
}
}
});
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// Optionally enable public read access.
// defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
After I ran the code, I got Parse: failure!
I don't know what is wrong with my code
.server("http://localhost:1337/parse/")
When you run the code in your app, localhost means "this device" AKA your Android device, not the server.
You need to use the IP address of the server there (which will depend if you are using an emulator or a physical device).

IntentService is not being called

I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.
Here is the onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new TwitterDB(this);
String[] columns = new String[1];
columns[0] = TwitterDB.TEXT;
tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0);
tweets = (ListView)findViewById(R.id.listtwitter);
tweets.setAdapter(tAdapter);
Intent intent = new Intent(this, SyncService.class);
intent.putExtra("action", SyncService.TWITTER_SYNC);
this.startService(intent);
}
Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.
public SyncService(){
super("SyncService");
twitterURI = Uri.parse(TWITTER_URI);
Log.i("SyncService", "Constructed");
}
#Override
public void onHandleIntent(Intent i){
int action = i.getIntExtra("action", -1);
Log.i("SyncService", "Retrieved intent");
switch (action){
case TWITTER_SYNC:
try{
url = new URL(twitterString);
conn = (HttpURLConnection)url.openConnection();
syncTwitterDB();
conn.disconnect();
}catch(MalformedURLException e){
Log.w("SyncService", "Twitter URL is no longer valid.");
e.printStackTrace();
}catch(IOException e){
Log.w("SyncService", "Twitter connection could not be established.");
e.printStackTrace();
}
break;
default:;
// invalid request
}
}
And here is the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name="SyncService"/>
</activity>
</application>
</manifest>
Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>
</application>
</manifest>
You need to define the path to the service in your manifest, simply putting the name is not sufficient.
Change
<service android:name="SyncService"/>
to
<service android:name=".SyncService"/>
if SyncService is in the root of your package, add respective folder before .SyncService if needed.

Categories

Resources