I have an ordering app, I am using SQLite to store what the user has added to it's cart. My problem is that how would I be able to upload all the data in the SQLite database to the server.
So for say, when the user presses the ORDER button, it will upload all the data in the SQLite to the databse.
I am doing this so that the person who will be delivering can retrieve and view the order details of the user, but I am just baffled and stuck on how would I be able to approach or do this.
If anyone has any insight or links that could give me an idea, or maybe provide me an alternative approach it would be great. Thanks in advance everybody! :D
WELL I SOLVED MY PROBLEM and this is my Solution for anyone who needs it!
Cursor data = db.getCartItems();
TotalOrderArray = new JSONArray();
data.moveToFirst();
while(data.isAfterLast() == false)
{
int totalColumn = data.getColumnCount();
TotalOrderObject = new JSONObject();
for (int i = 0; i < totalColumn; i++)
{
try {
TotalOrderObject.put(data.getColumnName(i), data.getString(i));
} catch (JSONException e) {
e.printStackTrace();
}
}
TotalOrderArray.put(TotalOrderObject);
data.moveToNext();
}
data.close();
it gets all of the data in the SQLite database as JSON and then I just upload it to the server. Thanks to everyone who helped!
Step 1: Just fetch the data from SQLite database.
Step 2: Encode this data to JSON format.
eg:-
ArrayList<Product_bean > all_product_list = db.getAllProductInCart(cart_id); // here get the products
JSONArray jarray = new JSONArray();
JSONObject product;
for(Product_bean bean : all_product_list){
product = new JSONObject();
product.put("product_id",bean.getP_id());
......
}
jarray.put(product);
jarray is your request jSON.
Step 3: Send the JSON to your Http... url.
Related
Error : java.lang.IllegalArgumentException: invalid type for ParseObject: class org.json.JSONObject
I have moved parse server on "centOS" and also database from parse.com to mangoDB. I'm getting Above error when I make below request from my android app.
Note : I'm using android parse sdk(v1.13.1)
I have tried to add jsonObject as arraylist using addAllUnique() method because I have to store jsonObject as datatype of "array" in parse database.
Below i share my code :
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("__type", "Pointer");
jsonObj.put("className", className);
jsonObj.put("objectId", objectId);
} catch (JSONException e1) {
e1.printStackTrace();
}
ParseUser user = ParseUser.getCurrentUser();
user.addAllUnique("Keyword",Arrays.asList(jsonObj));
user.saveInBackground();
I have also tried user.addAll() method insted of addAllUnique() but it's also not working.
Please help me to resolve this. Thanks
You can try something like this instead of JSON Object.
List<ParseObject> pointerList = new ArrayList<>();
pointerList.add(ParseObject.createWithoutData("YourClassName", "yourobjectId"));
pointerList.add(ParseObject.createWithoutData("YourClassName", "yourobjectId"));
...
user.addAllUnique("keyWord", pointerList);
user.saveInBackground();
Haven't tested the code yet. But technically, it should work.
Hope this helps :)
Hi guys i have already made login and register using volley library and data gets saved successfully as shown in the snap 1 below. Now my question is how to retrieve particular or specific row data for the particular user when he/she logins?? For example if user admin gets logged in how to fetch her entire row alone
saved data in server side
<?php
$conn = mysql_connect("localhost", "root", "");
if(isset($conn))
{
mysql_select_db('your_db', $conn);
}
else
{
echo 'Sorry,can not connect to database' ;
}
$userid = isset($_GET['id']) ? mysql_real_escape_string($_GET['id']) : "";
$qur = mysql_query("select usename,other_fields from `your_tbl` where userid= $userid");
$result =array();
while($r = mysql_fetch_array($qur)){
extract($r);
$result[] = array("usename" => $usename,"other_fields" => $other_fields);
}
$json =array("data"=>$result);
mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
You will have to use server side scripting. Webservice that will fetch that particular row of data.
The script should take your input parameters and give you the out put in Json Array or Object.
You then hit this script using volley and the response you receive can then be utilized for the next step.
//call volley get method and get data in success method
public void onSuccess(string response)
{
try {
String s = new String(response);
JSONObject jsonObject = new JSONObject(s);
if (jsonObject.has("data")) {
JSONArray country =jsonObject.getJSONArray("data");
for (int i = 0; i < country.length(); i++) {
JSONObject cnt = country.getJSONObject(i);
String res=cnt.getString("username"));
//now you can use res as per your requirement
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
I need to get followers list of currently logged in user. I saw this but how to make the URL request and manage the result. Can I make normal network request by using volley or something ? Any help is appreciated.
I was also facing the same problem. Then I found java wrapper for this functionality Twitter4j. It is very easy to use. Following are the lines of code which I use for storing followers ids in a list.
List<Long> ids = new ArrayList<Long>();
twitter4j.Twitter twitter1;
ConfigurationBuilder config =
new ConfigurationBuilder()
.setOAuthConsumerKey(custkey)
.setOAuthConsumerSecret(custsecret)
.setOAuthAccessToken(accesstoken)
.setOAuthAccessTokenSecret(accesssecret);
twitter1 = new TwitterFactory(config.build()).getInstance();
while(cursor != 0 && length > 0) {
System.out.println("in loop");
try {
IDs temp = twitter1.friendsFollowers().getFollowersIDs(username, cursor);
cursor = temp.getNextCursor();
tempids = temp.getIDs();
length = temp.getIDs().length;
} catch (twitter4j.TwitterException e) {
e.printStackTrace();
}
for (long id : tempids) {
ids.add(id);
}
}`
I also asked the same question in Twitter community. You can check it out at
https://twittercommunity.com/t/get-followers-of-a-user-using-twitter-android-sdk/37740
I'm trying to get the user's cover photo and show it at the top of a layout. I'm using AsyncTask to run the API call to Facebook. The code I'm using to get the Facebook data is
JSONObject json = null;
response = Utility.facebook.request("me?fields=cover");
json = Util.parseJson(response);
The exception that stops the thread comes from a json error on the next step because the returned json is empty, even though the request clears through. I can get a proper json using just "me" or "me/albums" or anything other than "me?fields=cover". When I comment out the last line, 'try' process finishes with no exceptions/errors.
Is there something wrong with the Facebook API or am I doing something wrong?
I personally prefer using FQL when dealing with User Profile. If you would like to give FQL a try, check the following piece of code. If you would like to stick to Graph API, see this answer: https://stackoverflow.com/a/12434640/450534
try {
String query = "SELECT pic_cover FROM user where uid = " + PUT_THE_USER_ID_HERE;
Bundle param = new Bundle();
param.putString("method", "fql.query");
param.putString("query", query);
String response = Utility.mFacebook.request(param);
JSONArray JAUser = new JSONArray(response);
for (int i = 0; i < JAUser.length(); i++) {
JSONObject JOUser = JAUser.getJSONObject(i);
// COVER PHOTO
if (JOUser.has("pic_cover")) {
String getCover = JOUser.getString("pic_cover");
if (getCover.equals("null")) {
String finalCover = null;
} else {
JSONObject JOCoverSource = JOUser.optJSONObject("pic_cover");
if (JOCoverSource.has("source")) {
String finalCover = JOCoverSource.getString("source");
} else {
String finalCover = null;
}
}
} else {
String finalCover = null;
}
}
} catch (Exception e) {
// TODO: handle exception
}
The above code already accounts for User's who do not have a Cover Photo set in their profiles and checks for its availability. With this code, you will have the URL to the Cover Photo and can then process it as you prefer.
NOTE: If you are fetching the logged in users cover photo, this piece of code SELECT pic_cover FROM user where uid = " + PUT_THE_USER_ID_HERE; can also be written as: SELECT pic_cover FROM user where uid = me()"; For the non-logged in user's cover photo, the above can be used as is.
Couple of things as a side note.
I use Fedor's Lazy Loading technique to load images in almost exclusively.
I recommend running the code block, mine or any other solution you choose, in an AsyncTask.
The reason for not getting any result can be found in the javadoc of request(String graphPath) method:
(...) this method blocks waiting for a network response, so do not
call it in a UI thread.
In your case, you should probably do the following synchronous call:
Bundle params = new Bundle();
params.putString("fields", "cover");
String result = Utility.facebook.request("me/", params);
Siddharth Lele is very correct in his answer, but I wanted to specify the actual reason for not getting any response in this case.
Note: Fetching Cover Photo using Facebook API and endpoint https://graph.facebook.com/me?fields=cover no longer works as on 20th Dec 2014.
It was supposed to give following response:
{
"cover": {
"cover_id": "10151008748223553",
"source": "http://sphotos-a.ak.fbcdn.net/hphotos-ak-ash4/s720x720/391237_10151008748223553_422785532_n.jpg",
"offset_y": 0
},
"id": "19292868552"
}
But now it just gives User's id:
{
"id": "19292868552"
}
Verified this using Graph Tool explorer v2.2 using me?fields=cover.
I have a web-service in .NET it's inserting and retrieving data's from database as object's..
I'll copy some part of web-service here..
[WebMethod(Description = "This is used to insert details into sql server")]
public string InsertDetails(DataTable myDetails, string STR1)
{
try
{
foreach (DataRow row in myDetails.Rows)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.InsertQry";
cmd.Parameters.Add(new SqlParameter("#P_no", row["POD_Number"].ToString()));
cmd.Parameters.Add(new SqlParameter("#P_id", STR1));
cmd.Parameters.Add(new SqlParameter("#P_author", Convert.ToInt64(row["P_author"])));
//opening the connection
cmd.Connection = sqlCon;
sqlCon.Open();
int retValue = cmd.ExecuteNonQuery();
sqlCon.Close();
if (retValue == 0)
return "false";
}
return "true";
}
catch (Exception ex)
{
ErrorLogger(ex);
return "false";
}
}
//-----------------------------------------------------------------------------------
[WebMethod(Description = "This is used to get details from sql server")]
public DataSet GetDetails(string STR1)
{
DataSet ds = new DataSet();
try
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.SelectQryFromDB";
//opening the connection
cmd.Connection = sqlCon;
sqlCon.Open();
ds.DataSetName = "myTbl";
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds,"myTbl");
sqlCon.Close();
return ds;
}
catch (Exception ex)
{
ErrorLogger(ex);
ds.DataSetName = "Error";
return ds;
}
}
//----------------------------
Any-one Help me by providing me the details how can i send those data's and retrieve data's in Android..This is my first application so i don't know much??
Please Provide me details so that i can insert and get data's using web-service??
I heard of kSOAP2 and i'm trying to accomplish this by using kSOAP2..
Working in Ksoap can be challenging sometimes. You first need to make sure your webservice is working fine and is giving you the proper result.
If that's done then you can consume the same from Android using ksoap library.
Here are few links that will help solve your problem:
1) How to call a local web service from Android mobile application will help you understand how you need to make a Ksoap call.
2) Inserting data into remote database from Android via ksoap
3) Returning data from server to Android