Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a http request by JSON in Android?

Tags:

json

android

http

I am new bee in Android , so the knowledge regarding android is not so vast, Ok lets come to the point, I need to make a login by username and password, for that I have to make http requset by JSON and in return the response will also come in JSON. So can anyone can HELP me please in it. THANKS.

like image 689
Jai Android Avatar asked Jan 27 '26 16:01

Jai Android


2 Answers

Try out this code

Button show_data;
JSONObject my_json_obj;
String path,firstname,lastname;
path = "http://192.168.101.123:255/services/services.php?id=9";
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
    HttpEntity  entity;
    HttpResponse response = null;
    HttpURLConnection urlconn;
    my_json_obj = new JSONObject();
    try
    {
        urlconn = (HttpURLConnection) new URL(path).openConnection();
        urlconn.setConnectTimeout(10000);
        urlconn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(urlconn.getOutputStream(), "UTF-8");

my_json_obj.put("sUserName", "test2");
      my_json_obj.put("sPassword", "123456");

writer.write(my_json_obj.toString());
        writer.close();

if(true)
        {
            String temp;
            temp = WebRequestCall(my_json_obj);
            //Log.i("Reply", temp);
        }
like image 72
Jaydeep Khamar Avatar answered Jan 30 '26 07:01

Jaydeep Khamar


I guess it is more the other way 'round: you send JSON data over HTTP.

Have a look at HttpURLConnection and the org.json.* classes.

like image 44
Heiko Rupp Avatar answered Jan 30 '26 08:01

Heiko Rupp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!