[Android] Json格式解析和文字图片传输
[Android] Json格式解析和文字图片传输
博客分类:Android json串拼写 key=关键字 value=https://www.it610.com/article/值例:登陆串
Java代码
文章图片
- userString = "eche.lau@gmail.com";
- password = "111111";
- JSONStringer userInfo = new JSONStringer().object()
- .key("email").value(userString)
- .key("password").value(password).endObject();
- JSONStringer json = new JSONStringer().object().key("request").value(userInfo).endObject();
{ request => { :email => "eche.lau@gmail.com",
:password => "111111"
} }
发送json格式请求方法
Java代码
文章图片
- public static JSONObject getJsonRequest(String url, Object json) {
- HttpPost request = new HttpPost(url);
- String retSrc = https://www.it610.com/article/null;
- JSONObject result = null;
- try {
- StringEntity se = new StringEntity(json.toString(), HTTP.UTF_8);
- //设置类型 编码
- se.setContentType("application/json; charset=UTF-8");
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
- "application/json; charset=UTF-8"));
- request.setEntity(se);
- // 发送请求
- HttpResponse httpResponse = new DefaultHttpClient()
- .execute(request);
- // 得到应答的字符串
- retSrc = https://www.it610.com/article/EntityUtils.toString(httpResponse.getEntity());
- // 生成 JSON 对象
- result = new JSONObject(retSrc);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result; //返回json对象
- }
Java代码
文章图片
- userRequest = getJsonRequest(Const.URL
- + "/sign_in.json", json);
- int status=userRequest.getInt("status"); //获取返回状态数字
- JSONObject user = userRequest.getJSONObject("user");
- int userId = user.getInt("id"); //解析int格式
- 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代码
文章图片
- public static JSONObject UpLoadRes(String url,String param,File bitmap ){
- String retSrc = https://www.it610.com/article/null;
- JSONObject result = null;
- //设置要访问的地址
- HttpPost httpRequestHttpPost = new HttpPost(url);
- MultipartEntity multipartEntity = new MultipartEntity(
- HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(HTTP.UTF_8));
- ContentBody contentBody = new FileBody(bitmap, "image/jpg");
- try {
- String newStr = new String(param.getBytes(), "UTF-8");
- //添加内容
- multipartEntity.addPart("email",new StringBody("eche.lau@gmail.com"));
- multipartEntity.addPart("request",new StringBody(newStr, Charset.forName(HTTP.UTF_8)));
- multipartEntity.addPart("res", contentBody);
- //把要传输的内容放到请求里面
- httpRequestHttpPost.setEntity(multipartEntity);
- //创建客户端对象
- HttpClienthttpClient = new DefaultHttpClient();
- //执行请求并得到返回结果
- HttpResponse httpResponse = httpClient.execute(httpRequestHttpPost);
- retSrc = https://www.it610.com/article/EntityUtils.toString(httpResponse.getEntity());
- // 生成 JSON 对象
- result = new JSONObject(retSrc);
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
【[Android] Json格式解析和文字图片传输】
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- 视频转换器哪种好用()
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)