Java Android - 如何将BASE64URL正文数据消息解码为UTF-8

时间:2016-07-25 16:15:31

标签: java android gmail-api

我从GMAIL API获得了这个JSON ouptup:Message Body Parts(获取消息gmail api)

文本/纯

PGRpdiBkaXI9Imx0ciI-PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQtZmFtaWx5OnZlcmRhbmEsc2Fucy1zZXJpZiI-SGVsbG_CoDxhIGhyZWY9Imh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS8iPmh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS88L2E-IGFuZCB0aGFuayB5b3UgZm9yIHlvdXIgaGVscCEuPC9kaXY-PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQtZmFtaWx5OnZlcmRhbmEsc2Fucy1zZXJpZiI-PGJyPjwvZGl2PjxkaXY-PGJyPjwvZGl2PjxkaXYgY2xhc3M9ImdtYWlsX3NpZ25hdHVyZSIgZGF0YS1zbWFydG1haWw9ImdtYWlsX3NpZ25hdHVyZSI-PGRpdiBkaXI9Imx0ciI-PGRpdj48ZGl2IGRpcj0ibHRyIj48ZGl2PjxkaXYgZGlyPSJsdHIiPjxkaXY-PGRpdiBkaXI9Imx0ciI-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-DQo8L2Rpdj4NCg==

text / html的

//result can be text/plain or text/html string from above
import android.util.Base64;
String orig = result.replaceAll("-","+").replaceAll("_","/");
byte[] a = Base64.decode(orig, Base64.DEFAULT); // byte[] result have content
String emailResult = new String(a,"UTF-8");
Log.i("EMAIL ", emailResult); //blank result
//or - also tried 
import com.google.api.client.util.Base64;
String c = new String(Base64.decodeBase64(result));
Log.i("EMAIL ", a); // a is blank

我无法解码消息,text / plain或text / html。我尝试了很多方法,但没有用。

ClearText(c => c.Class("MyCell").Index(0));

结果长度必须是4的倍数?我是否必须删除字符并将其替换为== s?

1 个答案:

答案 0 :(得分:0)

import android.util.Base64;

int flags = Base64.NO_WRAP | Base64.URL_SAFE;
byte[] data = Base64.decode(result, flags);
String emailResult = new String(data, "UTF-8");

Log.i("EMAIL ", emailResult);

// => EMAIL <div dir="ltr"> <div class="gmail_default" ...