Jai Android Jai Android - 11 months ago 71
HTTP Question

How to send a http request by JSON in Android?

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.

Answer Source

Try out this code

Button show_data;
JSONObject my_json_obj;
String path,firstname,lastname;
path = "";
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
    HttpEntity  entity;
    HttpResponse response = null;
    HttpURLConnection urlconn;
    my_json_obj = new JSONObject();
        urlconn = (HttpURLConnection) new URL(path).openConnection();

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

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


            String temp;
            temp = WebRequestCall(my_json_obj);
            //Log.i("Reply", temp);