Asked  6 Months ago    Answers:  5   Viewed   38 times

I want to send the following JSON text


to a web service and read the response. I know to how to read JSON. The problem is that the above JSON object must be sent in a variable name jason.

How can I do this from android? What are the steps such as creating request object, setting content headers, etc.



Android doesn't have special code for sending and receiving HTTP, you can use standard Java code. I'd recommend using the Apache HTTP client, which comes with Android. Here's a snippet of code I used to send an HTTP POST.

I don't understand what sending the object in a variable named "jason" has to do with anything. If you're not sure what exactly the server wants, consider writing a test program to send various strings to the server until you know what format it needs to be in.

int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
String postMessage="{}"; //HERE_YOUR_POST_STRING.
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);

HttpPost request = new HttpPost(serverUrl);
request.setEntity(new ByteArrayEntity(
HttpResponse response = client.execute(request);
Tuesday, June 1, 2021
answered 6 Months ago

Get complete form data as array and json stringify it.

var formData = JSON.stringify($("#myForm").serializeArray());

You can use it later in ajax. Or if you are not using ajax; put it in hidden textarea and pass to server. If this data is passed as json string via normal form data then you have to decode it using json_decode. You'll then get all data in an array.

  type: "POST",
  url: "serverUrl",
  data: formData,
  success: function(){},
  dataType: "json",
  contentType : "application/json"
Friday, June 4, 2021
answered 6 Months ago

Just try with below code this will use to send simple post data using volley, just replace URL and parameter data.

StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                    new Response.Listener<String>() {
                        public void onResponse(String response) {
                    new Response.ErrorListener() {
                        public void onErrorResponse(VolleyError error) {
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put(KEY_EMAIL, email);
                    return params;


Friday, August 6, 2021
answered 4 Months ago

This seems to be a bug in MySql.

You can workaround it though with cast(true as json) e.g.:

SELECT json_object(
    'name', 'Piotr',
    'likesMysql', if(4 MOD 2 = 0, cast(TRUE as json), cast(FALSE as json))
Friday, August 20, 2021
answered 4 Months ago

You have runOnUiThread which runs on ui thread and you have this

  JSONObject json = jsonParser.makeHttpRequest(
                        url_product_detials, "GET", params);

From you comments above the code getting product details by making HTTP request. So you are making http request on the ui thread. You will get NetworkOnMainThreadException.

public final void runOnUiThread (Runnable action)

Added in API level 1

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.


action the action to run on the UI thread

So remove the runOnUiThread and update ui in onPreExecute and onPostExecute. Make HTTp request in doInbackground.

Tuesday, October 26, 2021
Silver Light
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :