好的,所以当我在浏览器上尝试这个时,我能够在表格中插入参数。
http://example.com/webservice/?value=[{
"table": "people",
"operation": "insert",
"params": [
{
"age": 8,
"name": "john",
"last_name": "johnson"
}
],
"transactionCompleted": true
}]
如何用Java做这个问题是我的问题,我在哪里放置transactionCompleted的布尔值?
public void main(String[] args) throws Exception {
int age = 30;
String name = "john";
String lastName = "johnson";
URL url = new URL("http://www.example.com/webservice/?value=[{\"table\":\"people\",\"operation\":\"insert\"}]");
Map<String,Object> params = new LinkedHashMap<>();
params.put("age", age);
params.put("name", name);
params.put("last_name", lastName);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append(',');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for ( int c = in.read(); c != -1; c = in.read() )
System.out.print((char)c);
}
如果我想在Android中实现这一点,需要改变什么。
答案 0 :(得分:0)
试试这段代码,
HttpURLConnection urlConnection = null;
try {
// create connection
URL urlToRequest = new URL(serviceUrl);
urlConnection = (HttpURLConnection) urlToRequest
.openConnection();
urlConnection.setRequestProperty("Cookie", cookie);
switch (headerType) {
case 1:
urlConnection.setRequestProperty("Accept",
"application/json;odata=verbose");
break;
default:
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
String inputdata = "";
if (properties != null) {
inputdata = properties.getProperty(Constant.ID);
}
urlConnection.setRequestProperty("Accept",
"application/json;odata=verbose");
urlConnection.setRequestProperty("Content-Length",
Integer.toString(inputdata.getBytes().length));
urlConnection.setRequestProperty("Content-Type",
"application/json;odata=verbose");
urlConnection.setRequestProperty("Auth-Token", "e1cb16d0-751c-4485-ad83-b69e848fcdf3");
urlConnection.addRequestProperty("Authorization", "Bearer " + cookie);
DataOutputStream dataOutputStream = new DataOutputStream(
urlConnection.getOutputStream());
dataOutputStream.writeBytes(inputdata);
dataOutputStream.flush();
dataOutputStream.close();
break;
}
urlConnection.setConnectTimeout(30000);
urlConnection.setReadTimeout(100000);
// handle issues
statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
Log.d("URL Data ", "HTTP_UNAUTHORIZED");
} else if (statusCode != HttpURLConnection.HTTP_OK) {
Log.d("URL Data ", "HTTP_NOTOK " + statusCode);
}
// create JSON object from content
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
data = getResponseText(in);
}
适用于get和post方法。 您需要做的就是创建输入数据的json对象,并将其写在Property对象上,如下所示 -
JSONObject obj = new JSONObject();
obj.put("UserId","Deepak");
Property p = new Property();
p.put(Constant.ID,obj.toString();