1

I am new to android development I want parse the JSON Data, I want get response from server by posting this below JSON data

// login JSON
mtjson={
        "action":"login",
        "data"  :{
                "email":"ajit@globemoving.net",
                "password":"ajit@globemoving.net",
                "mobile":"9845031333"


            }
    }

// retrieve enquiry list JSON
mtjson={
        "action":"eq-list",
        "data"  :{
                "vid":"108"
            }
    }

// retrieve enquiry details JSON
mtjson={
        "action":"eq-details",
        "data"  :{
                "vid":"108",                    // vendor ID
                "eqid":"eq72873",                   // enquiry ID
                "state":"new"                   // state = new,quoted
            }
    }


// submit new bid for enquiry JSON
mtjson={
        "action":"bid",
        "data"  :{
                "vid":"108",                    // vendor ID
                "eqid":"eq72873",                   // enquiry ID
                "bid_amount":"5000",
                "mobile": "9876543623",
                "note":"dkjhdah"                
            }
    }

Hence please help with code with Httpurlconnection method. coz i am using new android studio apche.org is deprecated.

MainActivity

protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler webreq = new ServiceHandler();
            HashMap<String, String> params = new HashMap<>();


        JSONObject j1=new JSONObject();
        JSONObject j2=new JSONObject();

        try { j2.put("vid", "108");
            String e = j2.toString();
            j1.put("action", "eq-list");
            j1.put("data", e); }
        catch (JSONException e) {   e.printStackTrace(); }
       // String params = j1.toString();
       // Log.d("output", String.valueOf(j2));
        // Making a request to url and getting response
        String jsonStr = webreq.makeWebServiceCall(url, ServiceHandler.POST, params);

        Log.d("Response: ", "> " + jsonStr);

        studentList = ParseJSON(jsonStr);

        return null;`

Service Handler

public class ServiceHandler {

static String response = null;
public final static int GET = 1;
public final static int POST = 2;

//Constructor with no parameter
public ServiceHandler() {

}

/**
 * Making web service call
 *
 * @url - url to make request
 * @requestmethod - http request method
 */
public String makeWebServiceCall(String url, int requestmethod) {
    return this.makeWebServiceCall(url, requestmethod, null);
}

/**
 * Making service call
 *
 * @url - url to make request
 * @requestmethod - http request method
 * @params - http request params
 */
public String makeWebServiceCall(String urladdress, int requestmethod,
                                String params) {
    URL url;
    String response = "";
    try {
        url = new URL(urladdress);

        DataInputStream input;

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setDoInput(true);
        conn.setDoOutput(true);





        if (requestmethod == POST) {
            conn.setRequestMethod("POST");



        } else if (requestmethod == GET) {
            conn.setRequestMethod("GET");
        }


        if (params != null) {

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));

            StringBuilder result = new StringBuilder();
            boolean first = true;

            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (first)
                    first = false;
                else
                    result.append("&");

                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            }

            writer.write(result.toString());

            writer.flush();
            writer.close();
            os.close();
        }

        int responseCode = conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = br.readLine()) != null) {
                response += line;
            }
        } else {
            response = "";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

}

  • which json structure is to be sent as request and which to be retrieved, explain elaborately. – Mahalakshmi Jan 09 '16 at 08:25
  • mtjson={ "action":"eq-list", "data" :{ "vid":"108" } } this should be posted – Praveen Attigeri Jan 11 '16 at 07:04
  • // json structure data to post-{ "action":"eq-list", "data" :{ "vid":"108" } } JSONObject j1=new JSONObject(); JSONObject j2=new JSONObject(); try { j2.put("vid", "108"); j1.put("action", "eq-list"); j1.put("data", j2); } catch (JSONException e) { e.printStackTrace(); } and to post request check this link http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley – Mahalakshmi Jan 11 '16 at 07:15
  • this should be placed in post req no..if (requestmethod == POST) { conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Accept", "application/json"); JSONObject j1=new JSONObject();JSONObject j2=new JSONObject(); try { j2.put("vid", "108"); j1.put("action", "eq-list"); j1.put("data", j2):} catch (JSONException e){ e.printStackTrace(); } } else if (requestmethod == GET) { conn.setRequestMethod("GET"); } – Praveen Attigeri Jan 13 '16 at 11:49
  • hi can get ur mail id so that i can send code...please guide me – Praveen Attigeri Jan 18 '16 at 10:00
  • @Mahalakshmi while sending it should be string not in object, HashMap params = new HashMap<>(); so please tell me how pass it? – Praveen Attigeri Jan 27 '16 at 12:21
  • convert object to string and try, posting code will help resolve your issue quicker – Mahalakshmi Jan 27 '16 at 12:42

1 Answers1

0

If you can, I should really use a networking library to make it easier to send network requests, and parse the responses as objects in your model.

Have a look at:

I can provide more examples if you'd like but the documentations linked above are sufficient IMO.

Cheers!

Philippe A
  • 1,252
  • 9
  • 22