dop83362 2015-07-17 09:19
浏览 320
已采纳

可以使用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..

  • 写回答

1条回答 默认 最新

  • dpdx51205 2015-07-19 12:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘