如何使用 jackson 库在 android 中设置 headers 和 content-type (this restful .net web api)
How to set headers and content-type in android using jackson library (this restful .net web api)
这是 .Net 网站 api uri 是 http://sampleurlapi...../userRegistration/
如果使用 post 方法 JSON
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("ResNo", -1);
jsonobj.put("DateCreated", "12-12-2015");
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < finalSelectedArrayList.size(); i++) {
JSONObject resobject = new JSONObject();
resobject.put("StySK", finalSelectedArrayList.get(i).getParentSK());
resobject.put("TwrSK", finalSelectedArrayList.get(i).getTwrSK());
resobject .put("BlkSK", finalSelectedArrayList.get(i).getBlkSK());
resobject.put("FlrSK", finalSelectedArrayList.get(i).getFlrSK());
resobject.put("AptSK", finalSelectedArrayList.get(i).getAptSK());
resobject.put("IsDftUt", finalSelectedArrayList.get(i).getDefaultKey());
resobject .put(resobject);
}
// 建立网络服务连接
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
StringEntity entity = new StringEntity(jsonobj.toString());
request.setEntity(entity);
// Send request to service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
// Get the status of web service
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//If the result is returned as NULL, then make the validation to fail.
if ((strValidationResult = rd.readLine()) == null) {
strValidationResult = Boolean.FALSE.toString();
}
我有写JSON像这些。
如何使用 jackson 编写 json 并设置 content-type 并接受类型?
感谢提前!
i don't know what you ask but i consumed .net service this way
method calling here
int i = getconnect(email, pass);
get connect method
public Integer getconnect(String id, String pass) {
String result = null;
String i = null;
int statusCode=400;
String r="0";
String m="";
int s=0;
try {
HttpPost request = new HttpPost(url2);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer item = new JSONStringer()
.object()
.key("password").value(pass)
.key("username").value(email)
.endObject();
StringEntity entity = new StringEntity(item.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity1 = response.getEntity();
InputStream stream = entity1.getContent();
r = convertStreamToString(stream);
JSONObject jo = new JSONObject(r);
s= (Integer) jo.get("Flag");
Log.d("json result is:", r);
statusCode = response.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
Log.d("error", "code"+0);
}
return s;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
The Request set header like this problem is service side only
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
这是 .Net 网站 api uri 是 http://sampleurlapi...../userRegistration/
如果使用 post 方法 JSON
try {
JSONObject jsonobj = new JSONObject();
jsonobj.put("ResNo", -1);
jsonobj.put("DateCreated", "12-12-2015");
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < finalSelectedArrayList.size(); i++) {
JSONObject resobject = new JSONObject();
resobject.put("StySK", finalSelectedArrayList.get(i).getParentSK());
resobject.put("TwrSK", finalSelectedArrayList.get(i).getTwrSK());
resobject .put("BlkSK", finalSelectedArrayList.get(i).getBlkSK());
resobject.put("FlrSK", finalSelectedArrayList.get(i).getFlrSK());
resobject.put("AptSK", finalSelectedArrayList.get(i).getAptSK());
resobject.put("IsDftUt", finalSelectedArrayList.get(i).getDefaultKey());
resobject .put(resobject);
}
// 建立网络服务连接
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
StringEntity entity = new StringEntity(jsonobj.toString());
request.setEntity(entity);
// Send request to service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
// Get the status of web service
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//If the result is returned as NULL, then make the validation to fail.
if ((strValidationResult = rd.readLine()) == null) {
strValidationResult = Boolean.FALSE.toString();
}
我有写JSON像这些。
如何使用 jackson 编写 json 并设置 content-type 并接受类型?
感谢提前!
i don't know what you ask but i consumed .net service this way
method calling here
int i = getconnect(email, pass);
get connect method
public Integer getconnect(String id, String pass) {
String result = null;
String i = null;
int statusCode=400;
String r="0";
String m="";
int s=0;
try {
HttpPost request = new HttpPost(url2);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer item = new JSONStringer()
.object()
.key("password").value(pass)
.key("username").value(email)
.endObject();
StringEntity entity = new StringEntity(item.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity1 = response.getEntity();
InputStream stream = entity1.getContent();
r = convertStreamToString(stream);
JSONObject jo = new JSONObject(r);
s= (Integer) jo.get("Flag");
Log.d("json result is:", r);
statusCode = response.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
Log.d("error", "code"+0);
}
return s;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
The Request set header like this problem is service side only
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");