添加修改用户头像,昵称

This commit is contained in:
huangzhuanghua
2016-09-18 11:57:01 +08:00
parent a09c8e329a
commit a80c577f1f
11 changed files with 411 additions and 75 deletions

View File

@ -217,21 +217,21 @@ public class FileUtils {
* post method must write something to the connection
*/
connection.setDoOutput(true);
/* Read from the connection. Default is true. */
// Read from the connection. Default is true.
connection.setDoInput(true);
/* Post cannot use caches */
// Post cannot use caches
connection.setUseCaches(false);
/* Set the post method. Default is GET */
// Set the post method. Default is GET
connection.setRequestMethod("POST");
/* 设置请求属性 */
// 设置请求属性
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
if (token != null) {
connection.setRequestProperty("Auth", token);
connection.setRequestProperty("TOKEN", token);
}
/* 设置StrictMode 否则HTTPURLConnection连接失败因为这是在主进程中进行网络连接 */
// 设置StrictMode 否则HTTPURLConnection连接失败因为这是在主进程中进行网络连接
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
@ -240,38 +240,36 @@ public class FileUtils {
Utils.log("name = " + file.getName());
Utils.log("length = " + file.length());
}
/* 设置DataOutputStreamgetOutputStream中默认调用connect() */
// 设置DataOutputStreamgetOutputStream中默认调用connect()
DataOutputStream dos = new DataOutputStream(
connection.getOutputStream()); // output
// to
// the
// connection
// to the connection
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; "
+ "name=\"Filedata\";filename=\"" + file.getName() + "\""
+ end);
dos.writeBytes(end);
/* 取得文件的FileInputStream */
// 取得文件的FileInputStream
FileInputStream fStream = new FileInputStream(file);
/* 设置每次写入8192bytes */
// 设置每次写入8192bytes
int bufferSize = 8192;
byte[] buffer = new byte[bufferSize]; // 8k
int length = -1;
/* 从文件读取数据至缓冲区 */
// 从文件读取数据至缓冲区
while ((length = fStream.read(buffer)) != -1) {
/* 将资料写入DataOutputStream中 */
// 将资料写入DataOutputStream中
dos.write(buffer, 0, length);
}
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
/* 关闭流写入的东西自动生成Http正文 */
// 关闭流写入的东西自动生成Http正文
fStream.close();
/* 关闭DataOutputStream */
// 关闭DataOutputStream
dos.close();
/* 从返回的输入流读取响应信息 */
InputStream is = connection.getInputStream(); // input from the
// connection
// 从返回的输入流读取响应信息
InputStream is = connection.getInputStream(); // input from the connection
// 正式建立HTTP连接
Utils.log("con.getResponseCode() = " + connection.getResponseCode());
int ch;
@ -279,18 +277,16 @@ public class FileUtils {
while ((ch = is.read()) != -1) {
b.append((char) ch);
}
/* 显示网页响应内容 */
// 显示网页响应内容
Utils.log("content = " + b.toString().trim());
// {"status":"success","url":"http:\/\/api.ghzhushou.com\/temp\/5688e548d7859e6f278b4567.jpg"}
// {"icon":"http:\/\/api.ghzhushou.com\/temp\/5688e548d7859e6f278b4567.jpg"}
if (connection.getResponseCode() == HttpStatus.SC_OK) {
JSONObject response = new JSONObject(b.toString().trim());
if ("success".equals(response.getString("status"))) {
return response.getString("url");
}
return response.getString("icon");
}
} catch (Exception e) {
/* 显示异常信息 */
// 显示异常信息
Utils.log("Fail:" + e);
}
return null;