无法以json格式将图像字节数组编码字符串发送到服务器

时间:2015-02-05 05:28:09

标签: java php android arrays json

我是通过从图库中获取图像从活动发送来自设备的图像,然后将其转换为字节数组和编码字符串。

Uri selectedImageUri = data.getData();
                    selectedImagePath = getPath(selectedImageUri);
                    System.out.println("Image Path : " + selectedImagePath);

                    //Log.d("gallery ---- > ", "" + data.getExtras().get("data"));

                    imageView.setImageURI(selectedImageUri);
                    imageView.setMaxHeight(200);
                    imageView.setMaxWidth(200);

                    imageView.setDrawingCacheEnabled(true);


                    BitmapFactory.Options options0 = new BitmapFactory.Options();
                    options0.inSampleSize = 2;
                    // options.inJustDecodeBounds = true;
                    options0.inScaled = false;
                    options0.inDither = false;
                    options0.inPreferredConfig = Bitmap.Config.ARGB_8888;

                    bmp = BitmapFactory.decodeFile(selectedImagePath);

                    ByteArrayOutputStream baos0 = new ByteArrayOutputStream();


                    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos0);
                    byte[] imageBytes0 = baos0.toByteArray();




                    Log.e("Byte Array:", imageBytes0.toString());

                    String encodedImage =Base64.encodeToString(imageBytes0,Base64.URL_SAFE);
  • 但是,当我通过URL传递此字符串以发送到服务器(ASP.NET)时,它在HTTP连接中提供异常,

JSONParser类

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {


    }

    public JSONArray getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONArray jArray = null;

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpget = new HttpPost(url);
            HttpResponse response = httpclient.execute(httpget);
            //  response = 
            HttpEntity entity = response.getEntity();
            is = entity.getContent(); 

            if(is!=null) {

                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }

                is.close();
                result=sb.toString();
            }
            jArray = new JSONArray(result);    
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return jArray;
    }

}
  • 这一行有例外,

HttpPost httpget = new HttpPost(url); HttpResponse response = httpclient.execute(httpget);

Logcat跟踪

java.lang.IllegalArgumentException:索引161处的查询中的非法字符

图像字节数组编码的字符串在URL中大得多,可能是因为没有建立连接。

请建议我解决这个问题..

提前致谢

0 个答案:

没有答案