我的私人课程已实施AsynkTask<Void,Void,Void>
。
在AsynkTask
我通过 POST 请求(UTF-8 类型)向服务器发送带有用户信息的 JSON 时:
{
"action": "user:auth",
"body": {
"email": "login",
"password": "password"
},
"_client_software": "…"
}
以及带有用于授权会话的数据的响应 JSON:
{
"errorCode": 0,
"body": {
"sessionid": "fEfZEKATEPhC3EdIrqgrsWEb9fs5WWDT"
}
};
哪里errorCode = 0 - Ok
。
但是来自服务器的响应是 Deflate 压缩。
如何从 deflate 压缩中解压缩 JSON 字符串?
课程请求者:
public class Requestor {
public static String AUTH_ACTION = "user:auth";
public static final String URL = "https://example.com";
/* {
"action": "user:auth",
"body": {
"email": "login",
"password": "password"
},
"_client_software": "…"
}
*/
public JsonObject jsonAuthObject() {
JsonObject obj = new JsonObject();
obj.addProperty("action", "user:auth");
obj.addProperty("_client_software", "android_ics");
JsonObject body = new JsonObject();
body.addProperty("email", "user@domain.com");
body.addProperty("password", "pass12345678");
obj.add("body", body);
return obj;
}
// public HttpResponse makeRequest(JsonObject jsonObject) throws Exception {
// //instantiates httpclient to make request
// DefaultHttpClient httpclient = new DefaultHttpClient();
//
// //url with the post data
// HttpPost httpost = new HttpPost(URL);
//
// //convert parameters into JSON object
// JsonObject holder = jsonObject;
// Log.d("MyLog", "holder = " + holder.toString());
// //passes the results to a string builder/entity
// StringEntity se = new StringEntity(holder.toString(), HTTP.UTF_8);
//
// //sets the post request as the resulting string
// httpost.setEntity(se);
// //sets a request header so the page receving the request
// //will know what to do with it
// httpost.setHeader("Accept", "application/json");
// httpost.setHeader("Content-type", "application/json");
//
// return new DefaultHttpClient().execute(httpost);
// }
//
// public byte[] compress(String string) throws IOException {
// ByteArrayOutputStream os = new ByteArrayOutputStream(string.length());
// GZIPOutputStream gos = new GZIPOutputStream(os);
// gos.write(string.getBytes());
// gos.close();
// byte[] compressed = os.toByteArray();
// os.close();
// return compressed;
// }
//
// public String decompress(byte[] compressed) throws IOException {
// final int BUFFER_SIZE = 32;
// ByteArrayInputStream is = new ByteArrayInputStream(compressed);
// GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
// StringBuilder string = new StringBuilder();
// byte[] data = new byte[BUFFER_SIZE];
// int bytesRead;
// while ((bytesRead = gis.read(data)) != -1) {
// string.append(new String(data, 0, bytesRead));
// }
// gis.close();
// is.close();
// return string.toString();
// }
private String readAll(BufferedReader rd) throws IOException {
StringBuilder sb = new StringBuilder();
String inputLine;
while ((inputLine = rd.readLine()) != null) {
sb.append(inputLine);
}
return sb.toString();
}
public void readJsonFromUrl(String url) throws IOException {
InputStream is = null;
is.read(url.getBytes(), 0, url.length());
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(new GZIPInputStream(is), Charset.forName("UTF-8")));
String jsonText = readAll(rd);
System.out.println(jsonText);
} finally {
is.close();
}
}
public String makeRequest(JsonObject jsonObject) throws Exception {
//instantiates httpclient to make request
DefaultHttpClient httpclient = new DefaultHttpClient();
//url with the post data
HttpPost httpost = new HttpPost(URL);
//convert parameters into JSON object
JsonObject holder = jsonObject;
Log.d("MyLog", "holder = " + holder.toString());
//passes the results to a string builder/entity
StringEntity se = new StringEntity(holder.toString(), HTTP.UTF_8);
//sets the post request as the resulting string
httpost.setEntity(se);
//sets a request header so the page receving the request
//will know what to do with it
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();
StringBuilder stringBuilder = null;
try {
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
stringBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
Log.d("MyLog", "sb = " + stringBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
}
活动:
public class MainActivity extends Activity {
private static final String TAG = "A";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AuthAsynkTask authAsyncTask = new AuthAsynkTask();
authAsyncTask.execute();
}
private class AuthAsynkTask extends AsyncTask<Void, Void, Void> {
private static final String TAG = "MyLog";
@Override
protected Void doInBackground(Void... params) {
Requestor requestor = new Requestor();
try {
String jsonText = requestor.makeRequest(requestor.jsonAuthObject());
Log.d("MyLog","jsonText = "+jsonText.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
在日志中,文本编写如下:
D/MyLOg﹕ sb = x��VJ-*�/r�OIU�2�QJ�O�T��V*N-.����LQ�Rr�(+�5��.vO1t˵4��q2�L��OJJ-S����`�
我该如何解决这个问题?