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 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题