Get request error in resting framework - android

I am creating an android client which connects to the web service and gets a response code. The problem is the android app crashes whenever I run the following code:
RequestParams params = new BasicRequestParams();
params.add("client_id", "24f8b46fc9db409012830ca264ad7bcf");
params.add("response_type", "code");
ServiceResponse response=Resting.get("http://pricewatch.ap01.aws.af.cm/api/pricewatch/oAuth/auth",80,params);
IContentData contentData = response.getContentData();
String content = (String) contentData.getContent();
JSONObject jsonObject = new JSONObject(content);
String code = (String) jsonObject.get("code");
return code;
It generates a java.lang.NullPointerException with these line:
IContentData contentData = response.getContentData();
Could someone help with these? I've been stuck with these. Thanks in advance!

I run this code many times in the emulator but never worked. However, when I run this in an android device it worked! NullPointerException happens when it doesn't get anything or any response in the server. But what I don't understand is why does this happen? Is there a difference between an emulator and an android device in terms of executing an android application?

Related

Xamarin SqlServer cant get a connection

I'm building an app with the Entity Framework on Xamarin that lets me compare some data. But when I start my "fetchdata" function, I receive the Error:
System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)Snix_Connect (provider: SNI_PN7, error: 35 - SNI_ERROR_35)
I see many posts about Xamarin / Android & that it is not possible to get a connection to a SQL Server. Is there any way to fetch data from a SQL Server with .NET Core on Xamarin?
This is the string I put into SQL_Class folder with Sql_Common.cs
Fill up the brace brackets with actual parameters (removing the brace brakets too).
public static string SQL_connection_string = #"data source={server_address};initial catalog={database_name};user id={user_id};password={password};Connect Timeout={seconds}";
Then I access whenever I need it from any xamarin code just like we use in our asp.net c#
This works for me on my app without any issues.
using (SqlConnection Sql_Connection = new SqlConnection(Sql_Common.saralEHR_connection_string))
But as #Jason mentioned in his first reply, I too would get once again check the security part. I fexperienced before publishing Package to Google Play, they encrypt the App files with Hash Key Code and then only it gets upload to server
Yes it is possible (HuurrAYY!):
Im new in .net core, c# and so on and for me it was a hell of a work to get it working..
So here for the other noobs who are seeking for Help:
GuideĀ“s i used:
Building Android Apps with Entity Framework
https://medium.com/#yostane/data-persistence-in-xamarin-using-entity-framework-core-e3a58bdee9d1
https://blog.xamarin.com/building-android-apps-entity-framework/
Scaffolding
https://cmatskas.com/scaffolding-dbcontext-and-models-with-entityframework-core-2-0-and-the-cli/
How i did it:
Build your normal Xamarin app.
create new .net solution like in the tutorials (DONT WRITE YOUR Entity Framework CLASSES)
create a third solution what has to be a .net core console application
Scaffold your DB in your CONSOLE application move all created classes & folders in your "xamarin .net" solution & change the namespaces
Ready to Go!
Side Node: NuGets you need in every solution:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
[EDIT: NuGets you need in every solution]
I am doing this way (working snippet):
string connectionString = #"data source={server};initial catalog={database};user id={user};password={password};Connect Timeout=10";
string databaseTable = "{table name}";
string selectQuery = String.Format("SELECT count(*) as Orders FROM {0}", databaseTable);
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
//open connection
connection.Open();
SqlCommand command = new SqlCommand(selectQuery, connection);
command.Connection = connection;
command.CommandText = selectQuery;
var result = command.ExecuteScalar().ToString();
//check if there is result
if(result != null)
{
OrdersLabel.Text = result;
}
}
}
catch (Exception ex)
{
OrdersLabel.Text = ex.Message;
}
It is working fine, but API call more elegant.
I hope it helps.

Mailcore2 on Android hangs on MessageBuilder.setTo() when sending a message

I've got the Mailcore2 demo app working well. But in trying to replicate the iOS sample code for sending an email on Android, I find that setting the list of To addresses on the MessageBuilder object hangs. The call to setFrom() works fine. You can see the iOS sample code here:
http://libmailcore.com/
Here's my code:
Address fromAddress = Address.addressWithDisplayName(MessagesSyncManager.singleton().displayName,
MessagesSyncManager.singleton().user);
String stringToAddress = mEditTo.getText().toString();
Address toAddress = Address.addressWithMailbox(stringToAddress);
List<Address> toList = new ArrayList<Address>();
toList.add(toAddress);
Date now = new Date();
MessageBuilder builder = new MessageBuilder();
builder.header().setFrom(fromAddress);
builder.header().setTo(toList);
I've tried putting setTo() before setFrom(), but it still hangs. In the debugger I've validated that stringToAddress is a correctly formatted email address, and that toAddress and toList get created correctly. I've tried creating the To address using Address.addressWithDisplayName(), and the same thing happens. When setTo() hangs, the emulator shows a message box that says "Unfortunately, MailExample has stopped." I'm using Android Studio with the Nexus 5 API 21 x86 Android 5.0.2 emulator.

Google Spreadsheet API - Java - com.google.gdata.util.ParseException: Unrecognized content type:application/binary

I am using Google Spreadsheet API in my simple Android application. This is the piece of code:
URL spreadSheetUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetQuery query = new SpreadsheetQuery(spreadSheetUrl);
query.setTitleQuery("xyz");
query.setTitleExact(true);
SpreadsheetFeed spreadSheetFeed = service.getFeed(query, SpreadsheetFeed.class);
This piece of code is called from my application's sync adapter.
I am getting this error:
com.google.gdata.util.ParseException: Unrecognized content type:application/binary
com.google.gdata.client.Service.parseResponseData(Service.java:2136)
com.google.gdata.client.Service.parseResponseData(Service.java:2098)
com.google.gdata.client.Service.getFeed(Service.java:1136)
com.google.gdata.client.Service.getFeed(Service.java:1077)
com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676)
com.google.gdata.client.Service.getFeed(Service.java:1034)
Can someone suggest how I can solve this issue?
It turns out that I did not set the user credentials before executing this piece of code.
service.setUserCredentials(user, password);
Adding this line helped solve this issue. Weird.

Webservice and Monodroid

I am consuming a webservice following the guide from this tutorial using Mono For Android using c#. Which is running perfectly fine.
Now i have created my own webservice and created a simple test method GetMyName(String Name) which is written in vb.net.
If i run this webservice from a remote machine VIA webbrowser it works fine. But its breaking on this line of code when run using Android Emulator. (This code is part of the automatically generated code Refference.cs)
public string GetMyName(string Name) {
object[] results = this.Invoke("GetMayName", new object[] {
Name});
return ((string)(results[0]));
}
I am calling my webservice like this
webserviceformobile.Service1 webservice = new webserviceformobile.Service1();
String myName = webservice.GetMyName("RIZWAN");
when i run webservice from browser it outputs the following line:
<string xmlns="http://webserviceformobile.com/">rizwan</string>
What i am doing wrong ? Is my webservice is returning data in wrong format ?
In example which is working fine i am calling webservice like this
com.cdyne.wsf.Weather we = new com.cdyne.wsf.Weather();
com.cdyne.wsf.WeatherReturn wr = new com.cdyne.wsf.WeatherReturn();
wr = we.GetCityWeatherByZIP("02138");
Can you make sure that your webservice is reachable from the emulator ? This is the exact problem that I was facing.

Getting a JSONException: end of input at character 0

I have an API in php, that sends data in JSON format.
I made the following code, that works fine when I am on wifi.
But when I want to download the data from the API when I am on 3g, I receive the following exception:
JSONException: End of input at character 0 of
I have no idea why it does work on wifi, but it doesn't on mobile internet.
My code:
JSONObject json = getJSONfromURL("http://api.myurl.com/users.json");
JSONArray objects = json.getJSONArray("objects");
db.setLockingEnabled(false);
db.beginTransaction();
for (int i = 0; i < objects.length(); i++) {
JSONObject e = objects.getJSONObject(i);
if(e.getString("UID") != "-1"){
ContentValues values = new ContentValues();
//DO DATABASE INSERT. REMOVED THIS CODE FOR READABILITY
alldata_mProgressDialog.incrementProgressBy(1);
}
}
Anyone that can help me out?
You are probably getting a blank response. Its not null but the response is empty. So you are getting this error and not a Nullpointer exception
Check if you are asking for permissions to use the Internet.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in the Android Manifest
Check out others at Permissions overview # https://developer.android.com.
May be you are getting default response with default values. This kind of error comes up when you are not sending your requests properly or sending with wrong parameters. Check This
Sometimes this error is caused as json encode function requires all incoming data to be UTF-8 encoded.
Try adding mysqli_set_charset($con, 'utf8'); into your php.
This kind of error comes up when response is null.Print Json respone in logcat
Log.e ( "response", "" + response ), or you can check response on postman also.
I got this error while debugging an android app on Android Studio via USB. Connection attempts failed with one tablet but not with my regular test tablet. It turned out that the failure was due to the fact that I hadn't enabled WIFI settings on the failing machine. So I enabled WIFI on it and it connected to server OK.

Categories

Resources