|
'm learning Xamaring Forms. I want to test connecting to a Web API. I have a an Asp.Net Framework Web API running and I can connect to to it.
However, in my Xamarin Forms page, when I call it, the call to GetAsync never returns:
public class MainPageViewModel : _ViewModelBase
{
private string url = "<a href="https:
public List<TestDataEntity> Items { get; set; }
public MainPageViewModel()
{
GetData();
}
private async Task GetData()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content);
}
}
catch (Exception e)
{
throw;
}
}
}
I can paste the URL into a browser and see the JSON results. If the WebAPI is NOT running the GetAsync throws.
Anyone have any thoughts on this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
It sounds like you have a deadlock somewhere. Try adding .ConfigureAwait(false) to the GetAsync call.
private async Task GetData()
{
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content);
}
}
catch (Exception e)
{
throw;
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well that helped, a bit.
Again, I'm just trying to create a small, simple xamarin forms example that connects to a web api. I've read these:
Consume a RESTful Web Service - Xamarin | Microsoft Docs
6 Steps to Consume ASP.Net Core Web API in Xamarin | Syncfusion Blogs
Here's what I have now:
private async Task GetData()
{
string url = $"https://127.0.0.1:44340/api/Test/GetAll";
try
{
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
Items = JsonConvert.DeserializeObject<List<TestDataEntity>>(content);
}
}
catch (Exception e)
{
throw;
}
}
My browser opens and shows the commented out url (https:/localhost:44340). Yet the call to GetAsync throws "{System.Net.WebException: Failed to connect to localhost/127.0.0.1:44340". When I try that URL I get the same exception.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Is the WebAPI project running? That looks like an IIS Express URL, which means you'll need to run the API project from Visual Studio for the URL to work.
Also, if you're using a self-signed certificate and you haven't added it to your local PC's trust store, you'll need some extra code to ignore the certificate error:
c# - Allowing Untrusted SSL Certificates with HttpClient - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes, the API is running. In the browser I can type localhost:44340/api/test/GetAll and it returns json data from my repo.
localhost:44340 is what VS gives me when I run the app. So I'm trying that same URL in the Android app. Yet the exception shows the other IP address
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 21-Jul-20 13:16pm.
|
|
|
|
|
localhost is an alias for 127.0.0.1 , so it's resolving to the correct address.
Did you add the self-signed certificate to the local computer's trusted certificates store?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Did you add the self-signed certificate to the local computer's trusted certificates store?
What cert are you referring to?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
IIS Express issues a self-signed certificate to serve your application over HTTPS instead of HTTP.
When you open the site in a browser, you should see a warning that the certificate is untrusted.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm running from VS. When I start the app Chrome opens and I see JSON returned from my DAL. But no cert warnings.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
It sounds like you've added the certificate to Chrome's certificate store, or enabled chrome://flags/#allow-insecure-localhost to bypass the certificate warning.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
basic code for creating ah basci blog in the android studio with flutter project
|
|
|
|
|
Is that a question or a statement?
|
|
|
|
|
Richard MacCutchan wrote: Is that a question or a statement? It's a demand.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Oh dear, I was afraid it might be.
|
|
|
|
|
Hii guys i have a question that i create app . The app is able to send files like .mp3,.pdf etc.
But i want app should able to share apps also so what are the thing required . Can you suggest me.
Thank you.
|
|
|
|
|
AdityaSha wrote: Can you suggest me. You first need to decide, and describe, exactly what you mean by "app should able to share apps". What exactly do you want to share and who with?
|
|
|
|
|
I want to share app and files like .pdf and .mp3 files. To another device using wifi direct . I created app which can perform all operation which i want but it not able to send apps to another device . Basically a clone of ShareIT app.
|
|
|
|
|
Well I guess you need to research how to do the sharing. I can only assume that you need to post your files onto a web server somewhere that other people can get access to.
|
|
|
|
|
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projekat/com.example.projekat.EmailsActivity}: java.lang.IllegalArgumentException: Unknown URI: content:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2805)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2883)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1613)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:445)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Caused by: java.lang.IllegalArgumentException: Unknown URI: content:
at com.example.projekat.database.ContactsDBContentProvider.insert(ContactsDBContentProvider.java:79)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:265)
at android.content.ContentResolver.insert(ContentResolver.java:1539)
at com.example.projekat.tools.EmailsUtil.InitDB(EmailsUtil.java:23)
at com.example.projekat.EmailsActivity.onCreate(EmailsActivity.java:73)
at android.app.Activity.performCreate(Activity.java:7109)
at android.app.Activity.performCreate(Activity.java:7100)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2758)
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projekat">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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">
<service android:name=".sync.SyncSerivce" />
<receiver android:name=".sync.SyncReciever" />
<activity android:name=".MainActivity"></activity>
<activity
android:name=".SplashActivity"
android:label="Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LogInActivity"
android:label="Login activity" />
<activity
android:name=".ContactActivity"
android:label="Contact"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".CreateContactActivity"
android:label="Create contact"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".ContactsActivity"
android:label="Contacts"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".ProfileActivity"
android:label="Profile"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".SettingsActivity"
android:label="Settings"
android:parentActivityName=".MainActivity"></activity>
<activity
android:name=".FolderActivity"
android:label="Folder"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".FoldersActivity"
android:label="Folders"
android:parentActivityName=".EmailsActivity" />
<activity
android:name=".EmailActivity"
android:label="Email"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".EmailsActivity"
android:label="Emails"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".CreateFolderActivity"
android:label="Create folder"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<activity
android:name=".CreateEmailActivity"
android:label="Create Email"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
<provider
android:authorities="com.example.projekat"
android:name="com.example.projekat.database.ContactsDBContentProvider"
android:exported ="false"/>
<provider
android:authorities="com.example.projekat"
android:name="com.example.projekat.database.EmailsDBContentProvider"
android:exported ="false"/>
</application>
</manifest>
public class EmailsDBContentProvider extends ContentProvider {
private EmailsSQLiteHelper database;
private static final int EMAILS = 11;
private static final int EMAILS_ID = 21;
private static final String AUTHORITY = "com.example.projekat";
private static final String EMAILS_PATH = "emails.db";
public static final Uri CONTENT_URI_EMAILS = Uri.parse("content://" + AUTHORITY + "/" + EMAILS_PATH);
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, EMAILS_PATH, EMAILS);
sURIMatcher.addURI(AUTHORITY, EMAILS_PATH + "/#", EMAILS_ID);
}
@Override
public boolean onCreate() {
database = new EmailsSQLiteHelper(getContext());
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case EMAILS_ID:
queryBuilder.appendWhere(EmailsSQLiteHelper.COLUMN_ID + "="
+ uri.getLastPathSegment());
case EMAILS:
queryBuilder.setTables(EmailsSQLiteHelper.TABLE_EMAILS);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase db = database.getWritableDatabase();
Cursor cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri retVal = null;
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
long id = 0;
switch (uriType) {
case EMAILS:
id = sqlDB.insert(EmailsSQLiteHelper.TABLE_EMAILS, null, values);
retVal = Uri.parse(EMAILS_PATH + "/" + id);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return retVal;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
long id = 0;
int rowsDeleted = 0;
switch (uriType) {
case EMAILS:
rowsDeleted = sqlDB.delete(EmailsSQLiteHelper.TABLE_EMAILS,
selection,
selectionArgs);
break;
case EMAILS_ID:
String idEmails = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsDeleted = sqlDB.delete(EmailsSQLiteHelper.TABLE_EMAILS,
EmailsSQLiteHelper.COLUMN_ID + "=" + idEmails,
null);
} else {
rowsDeleted = sqlDB.delete(EmailsSQLiteHelper.TABLE_EMAILS, EmailsSQLiteHelper.COLUMN_ID + "=" + idEmails
+ " and "
+ selection, selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
long id = 0;
int rowsUpdated = 0;
switch (uriType) {
case EMAILS:
rowsUpdated = sqlDB.update(EmailsSQLiteHelper.TABLE_EMAILS,
values,
selection,
selectionArgs);
break;
case EMAILS_ID:
String idEmails = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsUpdated = sqlDB.update(EmailsSQLiteHelper.TABLE_EMAILS,
values,
EmailsSQLiteHelper.COLUMN_ID + "=" + idEmails,
null);
} else {
rowsUpdated = sqlDB.update(EmailsSQLiteHelper.TABLE_EMAILS,
values,
EmailsSQLiteHelper.COLUMN_ID + "=" + idEmails
+ " and "
+ selection,
selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsUpdated;
}
}
public class EmailsSQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_EMAILS = "Emails";
public static final String COLUMN_ID = "id";
public static final String COLUMN_OD = "od";
public static final String COLUMN_DATETIME = "dateTime";
public static final String COLUMN_SUBJECT = "Subject";
public static final String COLUMN_CONTENT = "Content";
public static final String COLUMN_AVATAR = "avatar";
private static final String DATABASE_NAME = "emails.db";
private static final int DATABASE_VERSION = 1;
private static final String DB_CREATE = "create table "
+ TABLE_EMAILS + "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_OD + " text, "
+ COLUMN_DATETIME + " text, "
+ COLUMN_SUBJECT + " text, "
+ COLUMN_CONTENT + " text, "
+ COLUMN_AVATAR + " integer "
+ ")";
public EmailsSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EMAILS);
onCreate(db);
}
}
|
|
|
|
|
At a guess, since you have not shown where the error occurs, it is in the following block of your code:
switch (uriType) {
case EMAILS_ID:
queryBuilder.appendWhere(EmailsSQLiteHelper.COLUMN_ID + "="
+ uri.getLastPathSegment());
case EMAILS:
queryBuilder.setTables(EmailsSQLiteHelper.TABLE_EMAILS);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
So you need to find out what value is in the uriType variable, and why.
|
|
|
|
|
Hi everybody. I'm a new android learner I'm doing a project related to sqlite, I have 2 data sheets
a table contains folder names a table contains items 2 of these tables linked together by a foreign key. I don't know how to dis play folders and items on recyclview please help me
|
|
|
|
|
Member 14849785 wrote: I don't know how to dis play folders and items on recyclview Do you have an adapter bound to this view?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Here fl_iv is the frame layout containing the imageview which showcases the image. I have set up a button there which allows user to use the image as LOCK SCREEN wallpaper. id of that button is lock_screen. Please try to be descriptive as i am new to this language
I tried this code for lock screen but its not working.
lock_screen.setOnClickListener {
val wpManager = WallpaperManager.getInstance(this)
val myBitmap: Bitmap = fl_iv.drawToBitmap()
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
wpManager.setBitmap(myBitmap, null, true, WallpaperManager.FLAG_LOCK)
}
else{
return@setOnClickListener
}
} catch (ex: IOException) {
ex.printStackTrace()
}
}
|
|
|
|
|
VipulSh wrote: but its not working Unfortunately that does not explain what your problem is. Please edit your question and explain what happens when you run your code (assuming it is your code, and not something downloaded from the internet).
|
|
|
|
|
Yea i tried to write it according to android documentation. It does nothing. Not even an error. It just doesn't do anything. I want to click it and set my bitmap image as lock screen wallpaper but nothing happens. What i should change in it to make it work for me?
|
|
|
|