Android 如何将JSON数组作为Req发送到URL
我使用下面的代码将Android 如何将JSON数组作为Req发送到URL,android,Android,我使用下面的代码将Jsonarray对象发送到url。但是,它不起作用 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("urlname"); JSONObject json = new JSONObject(); JSONArray Text_Field_Array = new JSONArray(); JSONO
Json
array对象发送到url。但是,它不起作用
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("urlname");
JSONObject json = new JSONObject();
JSONArray Text_Field_Array = new JSONArray();
JSONObject json_Text_Field = new JSONObject();
json_Text_Field.put("pagenum", "1");
json_Text_Field.put("text", "text field 1");
json_Text_Field.put("font", "Arial");
json_Text_Field.put("fontSize", "18");
json_Text_Field.put("color", "#ff0000");
json_Text_Field.put("x", "100");
json_Text_Field.put("y", "0");
json_Text_Field.put("alignment", "left");
Text_Field_Array.put(json_Text_Field);
JSONArray image_Field_Array = new JSONArray();
JSONObject json_image_Field = new JSONObject();
json_image_Field.put("image", "http://clintons-test.redboxdigital.net/media/uploads/llll.png");
json_image_Field.put("pagenum", "1");
json_image_Field.put("scale", "1");
json_image_Field.put("rotation", "90");
json_image_Field.put("x", "100");
json_image_Field.put("y", "10");
json_image_Field.put("width", "100");
json_image_Field.put("height", "100");
image_Field_Array.put(json_image_Field);
json.put("images", image_Field_Array);
json.put("all_text_fields", Text_Field_Array);
json.put("sku", "CCI1022537");
StringEntity se = new StringEntity( "JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
Log.v("URI","==="+ httppost);
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
sb = new StringBuilder();
byte[] buffer = new byte[512];
int bytes_read = 0;
while((bytes_read = is.read(buffer, 0, buffer.length)) > 0)
{
sb.append( new String(buffer,0,bytes_read));
}
我使用上面的代码是通过考虑primalpop在中的答案
我认为问题在于:
StringEntity se = new StringEntity( "JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
如果有人知道如何将Json数组发送到url,请给我一些建议。提前感谢JSONObject json=new JSONObject();
JSONObject json=new JSONObject();
JSONArray Text_Field_Array = new JSONArray();
JSONObject json_Text_Field = new JSONObject();
json_Text_Field.put("pagenum", "1");
json_Text_Field.put("text", "text field 1");
json_Text_Field.put("font", "Arial");
json_Text_Field.put("fontSize", "18");
json_Text_Field.put("color", "ff0000");
json_Text_Field.put("x", "100");
json_Text_Field.put("y", "0");
json_Text_Field.put("alignment", "left");
Text_Field_Array.put(json_Text_Field);
JSONArray image_Field_Array = new JSONArray();
JSONObject json_image_Field = new JSONObject();
json_image_Field.put("image", "http://clintons-test.redboxdigital.net/media/uploads/llll.png");
json_image_Field.put("pagenum", "1");
json_image_Field.put("scale", "1");
json_image_Field.put("rotation", "90");
json_image_Field.put("x", "100");
json_image_Field.put("y", "10");
json_image_Field.put("width", "100");
json_image_Field.put("height", "100");
image_Field_Array.put(json_image_Field);
json.put("images", image_Field_Array);
json.put("all_text_fields", Text_Field_Array);
json.put("sku", "CCI1022537");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("carddata", json.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
JSONArray Text_Field_Array=新的JSONArray();
JSONObject json_Text_Field=新JSONObject();
json_Text_Field.put(“pagenum”,“1”);
json_Text_Field.put(“Text”,“Text Field 1”);
json_Text_Field.put(“字体”、“Arial”);
json_Text_Field.put(“fontSize”,“18”);
json_Text_Field.put(“颜色”、“ff0000”);
json_Text_Field.put(“x”,“100”);
json_Text_Field.put(“y”,“0”);
json_Text_Field.put(“对齐”、“左”);
Text\u Field\u Array.put(json\u Text\u Field);
JSONArray image_Field_Array=new JSONArray();
JSONObject json_image_字段=新建JSONObject();
json_image_Field.put(“image”http://clintons-test.redboxdigital.net/media/uploads/llll.png");
json_image_Field.put(“pagenum”,“1”);
json_image_Field.put(“比例”,“1”);
json_image_Field.put(“旋转”、“90”);
json_image_Field.put(“x”,“100”);
json_image_Field.put(“y”,“10”);
json_image_Field.put(“宽度”、“100”);
json_image_Field.put(“高度”、“100”);
image\u Field\u Array.put(json\u image\u Field);
put(“图像”,图像\字段\数组);
put(“所有文本字段”,文本字段数组);
json.put(“sku”、“CCI1022537”);
List-nvps=newarraylist();
add(新的BasicNameValuePair(“carddata”,json.toString());
setEntity(新的UrlEncodedFormEntity(nvps,HTTP.UTF_8));
通过使用上述代码,我们可以在android中以req的形式发送json格式的字符串