0

I want to send the json data which contains login information to the webservice using GET request. What I am doing right now is the following,

public JSONObject getJsonData(String url) {

        StringBuilder stringBuilder = new StringBuilder();
        JSONObject jsonObject = new JSONObject();
        JsonObject jsonSend = new JsonObject();
        jsonSend.addProperty("email","email@gmail.com");
        jsonSend.addProperty("password","*********");
        HttpURLConnection conn = null;
        try {
            URL url1 = new URL(url.toString());

            conn = (HttpURLConnection) url1
                    .openConnection();
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.setDoInput (true);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.connect();
            OutputStream out = null;
            out = new BufferedOutputStream(conn.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
            writer.write(jsonSend.toString());
            Log.e("jsondata", jsonSend.toString());
            writer.close();
            out.close();
            conn.getResponseCode();
            InputStreamReader in = new InputStreamReader(
                    conn.getInputStream());

            int b;
            while ((b = in.read()) != -1) {
                stringBuilder.append((char) b);
            }
            try {
                Log.e("stringbuilder",stringBuilder.toString());
                jsonObject = new JSONObject(stringBuilder.toString());
            } catch (JSONException je) {
                Toast.makeText(this, "Error while creating json", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
//            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            try {
//                Toast.makeText(getApplicationContext()," code", Toast.LENGTH_SHORT).show();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        finally {
            conn.disconnect();
        }
        return jsonObject;
    }

Even though I am specifying request method as GET, it's trying to insert the data in the webservice. The webservice has the php code which will accept the json data and return a json data if the user credentials are valid and it accepts GET request. Please let me know how I can solve this. Thanks..

Anusha Harish
  • 376
  • 3
  • 14

1 Answers1

0

You cant define the content type in the get() request type. So you cant send a json in get request. As you are able to sent headers in get request but in header you cant send the json file.

Akash
  • 621
  • 3
  • 17