【发布时间】:2015-03-12 12:13:00
【问题描述】:
如何使用名称值对将图像上传到服务器。上传大图像时此代码不起作用。在将实体设置为 httppost 之前出现错误
public void uploadImage(Bitmap bitmapImg, String session) {
Log.d("mylog","constructing byte array stream object");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
Log.d("mylog","compressing bimaph image");
bitmapImg.compress(Bitmap.CompressFormat.PNG,100, baos);
Log.d("mylog","converting bitmap to byte array"+baos);
byte [] b=baos.toByteArray();
Log.d("mylog","converting byte array to string "+b);
String temp=Base64.encodeToString(b, Base64.DEFAULT);
Log.d("mylog","constructing name value pair "+temp);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
Log.d("mylog","adding value to image in name value pair");
nameValuePairs.add(new BasicNameValuePair("avatar",temp));
Log.d("mylog","going to make request for upload image");
json=jsonParser.getJsonForUpload("http://10.0.2.2/myweb/phototest.php","POST",nameValuePairs,session);
Log.d("mylog","json parsed sucessfully "+json);
jsonp=new JsonProvider(json);
}
public JSONObject getJsonForUpload(String url, String method,
final List<NameValuePair> params, final String session) {
final String sess = session,Url=url;
final List<NameValuePair> param = params;
Log.d("mylog", "getjsonupload method called");
try {
Log.d("mylog", "inside try");
if (method.equals("POST")) {
Log.d("mylog", "inside post");
Thread t = new Thread() {
public void run() {
try {
HttpPost httpPost = new HttpPost(Url);
Log.d("mylog", "post setted going to set header");
httpPost.setHeader("CONTENT_TYPE", "MULTIPART/form-data");
httpPost.setHeader("ENCTYPE", "MULTIPART/form-data");
Log.d("mylog", "going to set entity");
Blockquote 在此之后控件跳到主要任务 httpPost.setEntity(new UrlEncodedFormEntity(param));
Log.d("mylog", "going ton set connection time out");
HttpConnectionParams.setConnectionTimeout(cparams, 10000);
HttpConnectionParams.setSoTimeout(cparams, 10000);
if (sess != null) {
Log.d("mylog", "going to set session id to header of post ");
Log.d("mylog", "the session PHPSESSID is" + sess);
httpPost.setHeader("Cookie", "PHPSESSID=" + sess + ";");
Log.d("mylog", "header setted sucessfully");
}
Log.d("mylog", "going to execute client");
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.d("mylog", "response obtained from the server sucessfully" + httpResponse);
HttpEntity httpEntity = httpResponse.getEntity();
String jsondata = EntityUtils.toString(httpEntity);
Log.d("mylog", "json parsing sucess " + jsondata);
jobj = new JSONObject(jsondata);
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.d("mylog", "Error " + e);
} catch (UnknownHostException e) {
e.printStackTrace();
Log.d("mylog", "error " + e);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.d("mylog", "Error " + e);
} catch (IOException e) {
e.printStackTrace();
Log.d("mylog", "Error " + e);
} catch (JSONException e) {
e.printStackTrace();
Log.d("mylog", "Error " + e);
}
}
};
Log.d("mylog", "going to start thread");
t.start();
t.join();
Log.d("mylog", "thread finished");
if (session == null) {
Log.d("mylog", "cookie not available ");
Log.d("mylog", "going to obtain cooke from http response");
try {
cookieStore = httpClient.getCookieStore();
iscooke = true;
Log.d("mylog", "cookie obtained sucessfully " + cookieStore);
} catch (Exception e) {
e.printStackTrace();
Log.d("mylog", "error obtaining cookie" + cookieStore);
}
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d("mylog", "cookie is empty");
} else {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("PHPSESSID")) {
sessionid = cookie.getValue();
Log.d("mylog", "session id successfully obtained " + sessionid);
}
}
}
} else {
Log.d("mylog", "cookie store already available ");
}
}
} catch (Exception e) {
e.printStackTrace();
Log.d("mylog", "Error " + e);
}
// return JSON String
Log.d("mylog", "json from json object is " + jobj);
return jobj;
}
【问题讨论】:
-
你正在使用base64string上传图片,不适合上传大图,因为它的字符串会变得很大,可能会报错