phil-brown/droidQuery

POST data to Web

Closed this issue · 23 comments

Please help me to post data to web.
I cannot configure data for posting.

.data(????????) <~~ can you example something for me, thanks too much

$.ajax(new AjaxOptions().url("http://www.example.com")
.type("post")
.dataType("text")
.data(????????)
.context(this)
.success(new Function() {
@OverRide
public void invoke($ droidQuery, Object... params) {
JSONObject json = (JSONObject) params[0];
//handle json response. Also see $.map(JSONObject) for parsing
}
})
.error(new Function() {
@OverRide
public void invoke($ droidQuery, Object... params) {
AjaxError e = (AjaxError) params[0];
Log.e("$", "Error: " + e.status + ": " + e.error);
}
}));

@Alehap there is an example of GET on the main page, and here's a POST example on StackOverflow.

To answer your questions directly, .data() expects a JSON String. For example:

.data("{\"name\":\"Phil\", \"career\", \"mobile developer\"}")

Thank you too much.
I used this function:

$.ajax(new AjaxOptions().url("http://group.vnbuzz.net/masoi_ws/server.php")
.type("POST")
.data("{"act":"login"}")
.dataType("json")
.context(this)
.success(new Function() {
@OverRide
public void invoke($ droidQuery, Object... params) {
droidQuery.alert((String) params[0]);
}
}).error(new Function() {
@OverRide
public void invoke($ droidQuery, Object... params) {
int statusCode = (Integer) params[1];
String error = (String) params[2];
Log.e("Ajax", statusCode + " " + error);
}
}));

And the success function has ever called.
But the result of error function is "200 OK" !?
Please help me to solve this problem, thanks too much :)

image

At web, I see this function do not post any data :(

04/04/2014 15:48:32
array(0) {
}


04/04/2014 15:49:40
array(0) {
}


04/04/2014 15:51:13
array(0) {
}


04/04/2014 15:54:49
array(0) {
}


@Alehap Looking at the code, this occurs when there has been a problem parsing the returned data. I will change the status code and message to reflect this.

To determine what exactly the problem is, add this method call to your Ajax chain: .debug(true). This will allow stack traces from errors to print to logcat. What error(s) do you see here?

Where should I add .debug(true)??

If I add in ajaxoptions, I've got error@@
$.ajax(new AjaxOptions().url("http://group.vnbuzz.net/masoi_ws/server.php")
.type("POST")
.dataType("json")
.data("{"name":"Phil", "career":"mobile developer"}")
.context(this)
.debug(true)
.success(new Function() {
@OverRide
public void invoke($ droidQuery, Object... params) {
droidQuery.alert((String) params[0]);
}
}).error(new Function() {
@OverRide
public void invoke($ droidQuery, Object... params) {
int statusCode = (Integer) params[1];
String error = (String) params[2];
Log.e("Ajax", statusCode + " " + error);
}
}));

P.s: I'm sure the error is $.ajax not send any post data, because I've var_dump($_POST) in PHP code to see details of POST data, and I've got nothing@@

Thanks for your support me too much

Add it anywhere, for example:

$.ajax(new AjaxOptions().url("http://group.vnbuzz.net/masoi_ws/server.php")
                        .type("POST")
                        .data("{\"act\":\"login\"}")
                        .dataType("json")
                        .success(new Function() {                                            
                            @Override
                             public void invoke($ droidQuery, Object... params) {
                                 droidQuery.alert((String) params[0]);
                            }
                        }).error(new Function() {
                            @Override
                             public void invoke($ droidQuery, Object... params) {
                                 Log.e("Ajax", params[0].toString());
                            }
                        }).debug(true));//add here

This will help see if we are having trouble parsing the returned data. This may be the case if, for example, the request returns a 404 Not Found String instead of JSON.

Also, I have used this library in several Android apps, and have not had trouble with posting data. I wouldn't think any newer changes would cause problems, but you could try using version an older version (such as Optimized for loops by changing for-each loops to use an index i.). If this works, please let me know and I will fix the current build ASAP.

I cannot add this :(
image

What version of droidQuery are you using? the debug method has been available since Aug 02, 2013 (commit b9e27b6694ed34cc528fcd3237e704aa4f340335).

I'm using version "phil-brown-droidQuery-1.1.0-2-g559e2ea.tar.gz" !?

After I change new droiquery.jar, I've got this :((

image

@Alehap use droidQuery-1.1.0-source.jar. This is packaged with its dependencies - which may be the problem.

nothing changes :'(

image

Ok - now the debug flag is working. The error is as I suspected - a problem with parsing the returned JSON. What happens if you change the dataType to "text": .dataType("text")? This should show what is being returned in your success function.

if I change it to "text", it show nothing (empty string).
And when I use var_dump($_POST) to see whatever posting to web, and I see this ajax do not post any data@@

Hmmm.. There is obviously some disconnect between the client and the server. Some things you can try:

  • Does droidQuery-1.0.7 work?
  • Are you sure the server code is working? Do GET requests or other actions work?
  • Is .data("{\"act\":\"login\"}") written just like this - or do you generate the String?
  • Does the server expect JSON to be formatted differently? Some servers will work with JSON formatted like "{act:'login'}".

Also - I assumed that since both the android client and the server are responding to some events, that the INTERNET permission is set in the Manifest.

It doesn't work too :(
I'm sure the server code is working correctly. Because I'm using a web client to connect to this web service. Now I want to make a mobile app@@
I'm not need a method post json to server, I'm need only a simple post method such as when you login at any website@@

@Alehap In one of my published apps, which has a PHP server, I use this method:

public static AjaxOptions login(String emailAddress, String password) {
    return new AjaxOptions().url(Utilities.format("%s/%s/%s/login", baseURL, deviceType, version))
                            .type("POST")
                            .data(Utilities.format("{\"email\": \"%s\", \"password\": \"%s\" }", emailAddress, password));
}

I then make a call to this method, add the success and error callbacks, and invoke the request:

$.ajax(login(email, password).success(new Function() {
    public void invoke($ d, Object... args) {
        Log.i("Ajax", "Success: " + args[0].toString());
    }
}).error(new Function() {
    public void invoke($ d, Object... args) {
        Log.i("Ajax", args[0].toString());
    }
}));

And I have the following Manifest Permissions (only INTERNET should be needed):

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

This works well. You can see the results in this app.

hjc, in Manifest I've add it already :(


when I use method get, I can get data normally, but I cannot get data when post :(
this $.ajax cannot post data to my web service :(

I would revisit the web service. Maybe post a question on StackOverflow? If there is a problem with either of our code, someone with knowledge of both Android and backend technologies should be able to help. droidQuery is already in the StackOverflow tag database.

oh thank you too much :)

For sure. Please let me know how you solve the issue - then I can mark this as closed. Good luck!

@Alehap were you able to resolve this issue?

Hey i had the same problem i just need to set .contentType("application/json"), then it worked fine :)