1
在下面的代碼中,執行沒有超越HttpResponse語句。在顯示Toast之前,不會顯示它之後的內容。沒有任何異常。可以告訴我爲什麼?HttpResponse無法正常工作
我從圖庫中選取一張圖像即可獲得該Uri,甚至可以在ImageView中顯示。因此Uri沒有問題。
Uri selectedImage = data.getData();
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, baos);
byte ar[] = baos.toByteArray();
String s = Base64.encodeBytes(ar);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("My URL");
ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("image", s));
HttpEntity entity = new UrlEncodedFormEntity(list);
httppost.setEntity(entity);
Toast.makeText(this, "Upload",Toast.LENGTH_LONG).show();
HttpResponse response = httpclient.execute(httppost); // HERE
Toast.makeText(this, "Uploaded or not?", Toast.LENGTH_LONG).show();
java.io.InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result = br.readLine();
我曾經見過一些以前的問題,但沒有工作。
你看到例外logcat的?你是否在單獨的線程中運行此代碼,因爲網絡操作不允許在Android版本> 3.x – httpdispatch 2013-05-01 10:06:39
我在2.3.6版本上運行它,Logcat中沒有任何內容 – 2013-05-01 10:29:43