Volley jsonArray Request not working - android

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" />

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.

Unable to resolve host, no address associated with hostname

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);

AsyncTask Permission denied (missing INTERNET permission?)

I am trying use AsyncTask to extract an image over the internet and display on the RecyclerViewer but I am getting the following error message, which's captured from my Exception in my doInBackground from my RecyclerViewAdapter class:
08-02 00:11:25.608: E/ImageDownload(5753): Download failed: Permission denied (missing INTERNET permission?)
See full version of AsyncTask sub-class below:
class GetImageFromNet extends AsyncTask<String, Void, Bitmap> {
private RVAdapter.PersonViewHolder personViewHolder;
private String url = "http://ia.media-imdb.com/images/M/MV5BNDMyODU3ODk3Ml5BMl5BanBnXkFtZTgwNDc1ODkwNjE#._V1_SX300.jpg";
public GetImageFromNet(RVAdapter.PersonViewHolder personViewHolder){
this.personViewHolder = personViewHolder;
}
protected Bitmap doInBackground(String... params) {
try {
InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
return bitmap;
//NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute()
} catch (Exception e) {
Log.e("ImageDownload", "Download failed: " + e.getMessage());
}
return null;
}
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
personViewHolder.personPhoto.setImageBitmap(bitmap);
}
I have no idea why I am getting above error since I have already added the following permission access in my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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>
Please help!
Move your <uses-permission> elements to be immediate children of <manifest>, above your <application> element.
Update your manifest as:-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx" >
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>

How to write text file via emulator?

Yesterday i post this question and some one replied me by peace of codes but the problem is permission denied. i do not know why happen this error because i have allowed permission in my manifest.xml. below is peace of code.
This is java code
public void WriteText() {
EditText txt= (EditText) findViewById(R.id.txtwrite);
try {
BufferedWriter fos = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"File.txt"));
fos.write(txt.getText().toString().trim());
fos.close();
Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
}
}
This is manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
Please any one help me i was so Straggled about it.

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