How to request JSON object transfer on Android

Asked 2 years ago, Updated 2 years ago, 109 views

I would like to send the following JSON text {"Email":"[email protected]"Password":"123456"} to the web service and read the response. I know how to read JSON. The problem is that the above JSON object must be sent to a variable named Jason.

What do we do in an Android environment? What kind of procedures do I have to go through, such as creating a request object and setting a content header?

android post httprequest json

2022-09-22 12:53

1 Answers

There is no special code for sending and receiving HTTP on Android. You can just use the standard Java code. We recommend using Apache HTTP client for Android. Here I uploaded a piece of code that I use to transmit HTTP POST.

I don't understand what it has to do with transferring an object to a variable named "jason". If you don't know exactly what the server needs, try sending various string data to the server through a test program until you know what format the server needs.

 int TIMEOUT_MILLISEC = 10000; // = 10 seconds
String postMessage="{}"; //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);


2022-09-22 12:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.