[Android] Json格式解析和文字图片传输

[Android] Json格式解析和文字图片传输
博客分类:Android json串拼写 key=关键字 value=https://www.it610.com/article/值例:登陆串


Java代码[Android] Json格式解析和文字图片传输
文章图片

  1. userString = "eche.lau@gmail.com";
  2. password = "111111";
  3. JSONStringer userInfo = new JSONStringer().object()
  4. .key("email").value(userString)
  5. .key("password").value(password).endObject();
  6. JSONStringer json = new JSONStringer().object().key("request").value(userInfo).endObject();
拼写完成的格式是:

{
request => {
:email => "eche.lau@gmail.com",
:password => "111111"
}
}

发送json格式请求方法


Java代码[Android] Json格式解析和文字图片传输
文章图片
  1. public static JSONObject getJsonRequest(String url, Object json) {
  2. HttpPost request = new HttpPost(url);
  3. String retSrc = https://www.it610.com/article/null;
  4. JSONObject result = null;
  5. try {
  6. StringEntity se = new StringEntity(json.toString(), HTTP.UTF_8);
  7. //设置类型 编码
  8. se.setContentType("application/json; charset=UTF-8");
  9. se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
  10. "application/json; charset=UTF-8"));
  11. request.setEntity(se);
  12. // 发送请求
  13. HttpResponse httpResponse = new DefaultHttpClient()
  14. .execute(request);
  15. // 得到应答的字符串
  16. retSrc = https://www.it610.com/article/EntityUtils.toString(httpResponse.getEntity());
  17. // 生成 JSON 对象
  18. result = new JSONObject(retSrc);
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. return result; //返回json对象
  23. }
得到返回json对象并且解析

Java代码[Android] Json格式解析和文字图片传输
文章图片
  1. userRequest = getJsonRequest(Const.URL
  2. + "/sign_in.json", json);
  3. int status=userRequest.getInt("status"); //获取返回状态数字
  4. JSONObject user = userRequest.getJSONObject("user");
  5. int userId = user.getInt("id"); //解析int格式
  6. String nickName = user.getString("nickname"); //解析string格式
返回格式是:

{
:status => 200,
:user => {
:id => 8,
:nickname => "逐风林羽",
}
}
还有数据类型:

JSONArray users = data.getJSONArray("users");

for (int i = 0; i < users.length(); i++) {
JSONObject user = users.getJSONObject(i);
//然后再向上个例子一样解析user串
}

传输bitmap图片

Java代码[Android] Json格式解析和文字图片传输
文章图片
  1. public static JSONObject UpLoadRes(String url,String param,File bitmap ){
  2. String retSrc = https://www.it610.com/article/null;
  3. JSONObject result = null;
  4. //设置要访问的地址
  5. HttpPost httpRequestHttpPost = new HttpPost(url);
  6. MultipartEntity multipartEntity = new MultipartEntity(
  7. HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8));
  8. ContentBody contentBody = new FileBody(bitmap, "image/jpg");
  9. try {
  10. String newStr = new String(param.getBytes(), "UTF-8");
  11. //添加内容
  12. multipartEntity.addPart("email",new StringBody("eche.lau@gmail.com"));
  13. multipartEntity.addPart("request",new StringBody(newStr, Charset.forName(HTTP.UTF_8)));
  14. multipartEntity.addPart("res", contentBody);
  15. //把要传输的内容放到请求里面
  16. httpRequestHttpPost.setEntity(multipartEntity);
  17. //创建客户端对象
  18. HttpClienthttpClient = new DefaultHttpClient();
  19. //执行请求并得到返回结果
  20. HttpResponse httpResponse = httpClient.execute(httpRequestHttpPost);
  21. retSrc = https://www.it610.com/article/EntityUtils.toString(httpResponse.getEntity());
  22. // 生成 JSON 对象
  23. result = new JSONObject(retSrc);
  24. } catch (ClientProtocolException e) {
  25. // TODO Auto-generated catch block
  26. e.printStackTrace();
  27. } catch (IOException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. } catch (JSONException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34. return result;
  35. }

【[Android] Json格式解析和文字图片传输】

    推荐阅读