json 用base64 编码接收图片
if (mSocket != null) {
fi = mSocket.getInputStream();
byte[] bytes = new byte[256];
// TODO
int count;
if ((count = fi.read(bytes)) != -1) {
final String s = new String(bytes, 0, count, "utf-8");
JSONObject jsonObject = new JSONObject(s);
jsonText = "文字:" + jsonObject.getString("jsonText");
Log.d("wenzi", jsonText);
jsonImage = jsonObject.getString("image");
jsonImgPath = jsonObject.getString("imgPath");
Log.d(TAG, jsonImage.toString().length() + "");
byte[] b;
if (jsonImage != null) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(jsonImgPath);
b = new sun.misc.BASE64Decoder().decodeBuffer(jsonImage);
for (int i = 0; i < b.length; i++) {
if (b[i] < 0) {
b[i] += 256;
}
}
int len = 0;
fileOutputStream.write(b);
mBitMap = BitmapFactory.decodeByteArray(b, 0, b.length);//将byte[]转成bitmap
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.d("strToImg", e.getMessage());
}
}
Log.i(TAG, "received bytes " + s);
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText(jsonText);
mImageView.setImageBitmap(mBitMap);
}
});
}
运行出现Unterminated string at character 256 of {"image":"\/9j... 的异常,求指点