读书人

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

发布时间: 2013-11-08 17:51:56 作者: rapoo

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

json串拼写 key=关键字 value=值 ?例:登陆串

?

public static JSONObject UpLoadRes(String url,String param,File bitmap ){String retSrc = 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);         //创建客户端对象         HttpClient  httpClient = new DefaultHttpClient();         //执行请求并得到返回结果HttpResponse httpResponse = httpClient.execute(httpRequestHttpPost);retSrc = EntityUtils.toString(httpResponse.getEntity());// 生成 JSON 对象result = new JSONObject(retSrc);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}

?

?

读书人网 >JavaScript

热点推荐