可以使用GET请求将json数据发送到Web服务吗?

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..

查看全部
dop83362
dop83362
2015/07/17 09:19
  • android
  • httpurlconnection
  • json
  • php
  • web-services
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复