Asked  6 Months ago    Answers:  5   Viewed   38 times

I want to send the following JSON text

{"Email":"aaa@tbbb.com","Password":"123456"}

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.

 Answers

42

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(
    postMessage.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);
Tuesday, June 1, 2021
 
radmen
answered 6 Months ago
34

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.

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

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>() {
                        @Override
                        public void onResponse(String response) {
                            Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
                        }
                    }){
                @Override
                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put(KEY_USERNAME,username);
                    params.put(KEY_PASSWORD,password);
                    params.put(KEY_EMAIL, email);
                    return params;
                }

            };

            getRequestOtpPage().addToRequestQueue(stringRequest);
Friday, August 6, 2021
 
phi
answered 4 Months ago
phi
57

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
 
SubniC
answered 4 Months ago
57

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.

Parameters

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 :  
Share