Mysql Connection, how to request data correctly? - android

I'm trying to follow this tutorial in order to connect to an external database with android:
http://www.helloandroid.com/tutorials/connecting-mysql-database
However, I seem to receive two different errors when viewing the php / mysql connection code directly
and through logcat.
If I look at it through a browser, it comes up with the orange php error saying:
Undefined index: year
Found on line 6 with .$_REQUEST['year'].
If i debug the app and look at the logcat output, it seems to give a half error:
E/log_tag ( 403): Error parsing data org.json.JSONException: A JSONArray text m
ust start with '[' at character 6 of
On the php page, below the error, Json prints off the tables correctly, and echoing mysql errors are fine.
Can anyone help with the implementation of this function $_REQUEST please? (If that is the problem.)

Your output comes from the http request must be a well formed json string.
Obviously a PHP error like an Undefined index can break the json string and makes it unparsable by java.
You need to rid off the notice, for istance:
<?php
if (isset($_REQUEST['year']) && $_REQUEST['year'] == 'foo'){
//Do query and output back the json array
}
?>
BTW $_REQUEST is not a function. Is a superglobal associative array where all request variables (GET/POST/COOKIE) are stored.

Related

retrofit error when fetching pretty print json from php

I write a simple program for fetching some json data and I hit by this error
Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $
I searched in stackoverflow and I didn't get the answer
but I figured out this error is because of "JSON_PRETTY_PRINT" code in json encode or the pre tag maybe
.
I changed this :
echo "<pre>".json_encode($row,JSON_PRETTY_PRINT)."</pre>";
to this :
echo json_encode($row);
and it worked. But the printed json is really messy and ugly. Do you have any solution for this problem ?
Use retrofit library and probably you are sending an array but on parsing you parse it as an Object not an array, thats problem

Check hashMap key contain HTML tags

When we change URL to incorrect value then we get HTML type response from server side that we restore in hamhMap key value on the basis of "=". The hashMap key contain HTML tags like
"<html>\t<head>\t\t<title>CSRFRedirect</title>\t</head>\t<body>\t\t\t\t<form method"
Please guide how to handle this type of key?
check the response code of your api response. if it throws error code 500 it is server error , you can handle it
check here for more info

Python json/list to dictionary cast not working

I'm currently deveolping an Android application that has Django framework as it's server side.
When i'm posting a data of a new user to my database i am POSTing a multipart request that has a user part inside.
The user for some reason is represented as a list but when i take it out of the request.data['user'] it's a str instance (Yea i dont know why...)
When i fetch that str i started working on it with json package.
I looked up on the internet (to many places..) how to convert a string in json format to a dictionary.
What i found is that when you use the json.loads command it doesn't give a dict back but a str instance :)
This is the code on my server side when i enter the create function of the ModelViewSet that handles the creation of the user.
userJson = request.data['user']
userJson = json.dumps(userJson)
userJson = json.loads(userJson)
What i tried to do is to make a string of my own in JSON format and that called the json.loads() command which gave me the dict object..
There seems to be a problem with processing the str from the http request of django rest framework for some reason or there's something else i am not seeing.
I tried the following links -
Converting JSON String to Dictionary Not List
http://docs.python-guide.org/en/latest/scenarios/json/
Didn't worked also..
Now, i tried accessing the str i got from json.loads() like a dictionary in this way.
id = userJson['id']
Now lets say maybe i passed a wrong json format to the loads function, it should have thrown an exception..
The code above (getting the id) raised an exception of 'String indices must be integer' - it doesn't convert it to dict! LOL xD
Good note worth mentioning - I'm trying to convert the json to a dictionary so i could access it like this - dictObject['id']
Well i would really appreciate every help!
Thanks :)
For some reason , when i did this commands-
userJson = request.data['user']
userJson = json.loads(userJson)
userJson = json.loads(userJson)
What i got to have inside the userJson after the second json.loads(userJson) I got the actual dict object to the userJson member.
Appearently it is a bug.
21 January - another update, I truly was doing double Json encoding on the Android application so that was the reason for double json. loads()

Android: can not parse and view this code like _v("KkPXHvrswLPRwO=" s(-755 865) s(101) s(119)

I have trouble when I'm getting data in HTML type from website. A strange code appears like:
_v("KkPXHvrswLPRwO="+s(-755+865)+s(101)+s(119)+s(-227+259)+s(760-690)+s(598- 481)+s(708-598)+s(265-166)+s(116)+s(-642+747)+s(111)+s(-496+606)+"(\""+s(534-434)+s(-854+965)+s(99)+s(743-626)+s(109)+s(374-273)+s(110)+s(365-249)+s(46)+s(119)+s(5+109)+s(105)+s(573-457)+s(101)+s(40)+s(67)+s(169-55)+s(121)+s(112)+s(116)+s(-96+207)+s(873-799)+s(83)+s(-817+863)+s(1+64)+s(69)+s(910-827)+s(-767+813)+s(100)+s(101)+s(-651+750)+s(-700+814)+s(121)+s(112)+s(116)+s(-341+381)...;.
In web, it is a sentence. But When getting data, it will always return this. I used HTMLCleaner to get TagNode and parse. I returned about 10 nodes and some of them appear like this.
Thank you very much.

Ajax post data in android java

I am new t ajax, but quite familiar with android. I am converting a ajax program to android app. As a part of it, i need to post data to the server. Below is the given post command in ajax.
var postTo = 'xyz.php';
$.post(postTo,{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' } ,
function(data) {
if(data.success){
window.localStorage["sa_id"] = data.mid;
window.location="getempdb.html";
}
if(data.message) {
$('#output').html(data.message);
} else {
$('#output').html('Could not connect');
}
},'json');
I want to implement this in android but under very little from the above statements. Could anyone who is good at ajax help me out with this thing. As of now, i get the user name and telephone number as a edit text input. I need to send this to php using http client. I know how to send data using php, but do not know what format to send and whether its a string to send or as a json object to send. Please help in interpreting the above code and oblige.
Apparently, this uses UrlEncodedFormEntity if you are using HttpClient in android.
This is created by using a List of NameValuePair.
from the parameters to the $.post:
{employee_name: $('[name=employee_name]').val() , phone: $('[name=phone]').val(), employee_type: 'guest' }
You have to create a NameValuePair for employee_name, one for phone ... each of which is fetched from a HTML element name employee_name, phone ... This is where you put the values from your EditTexts.
It returns a JSON formatted String, which you have to parse (typically using JSONObject obj = new JSONObject(result); once you have fetched the result from the server)
In this JSON object, you have a key named success, which format is not specified, except you can assume things went well if it is present ; a key mid, and a key message.

Categories

Resources