可以使用 GET 请求将 json 数据发送到 Web 服务吗?
Can json data be sent to web service using GET request?
我想使用 GET 请求将包含登录信息的 json
数据发送到 webservice
。我现在正在做的是以下内容,
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;
}
即使我将请求方法指定为 GET,它也会尝试将数据插入 webservice
。 webservice
有 php 代码,如果用户凭据有效并且它接受 GET 请求。请让我知道如何解决这个问题。谢谢..
您不能在 get() 请求类型中定义内容类型。所以你不能在 get 请求中发送 json 。由于您可以在获取请求中发送 header,但在 header 中您无法发送 json 文件。
我想使用 GET 请求将包含登录信息的 json
数据发送到 webservice
。我现在正在做的是以下内容,
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;
}
即使我将请求方法指定为 GET,它也会尝试将数据插入 webservice
。 webservice
有 php 代码,如果用户凭据有效并且它接受 GET 请求。请让我知道如何解决这个问题。谢谢..
您不能在 get() 请求类型中定义内容类型。所以你不能在 get 请求中发送 json 。由于您可以在获取请求中发送 header,但在 header 中您无法发送 json 文件。