在Android中发送和下载图像

时间:2013-06-12 08:09:19

标签: php android mysql json

嘿伙计我将发送图像下载到数据库时遇到问题。

  

decode-decoder =返回false。这是logcat的错误

这是代码;解析JSON数据。我认为这是一个问题。我的节目应该  将照片从android发送到数据库。我使用Base64进行解码。在其他活动中,我可以通过解析数据下载此照片。

public class ParsingData extends AsyncTask<String, Void, JSONObject>{
    ProgressDialog progressDialog ;
    GetJSONListener getJSONListener;
    Context curContext;
    public ParsingData(Context context, GetJSONListener listener){
        this.getJSONListener = listener;
        curContext = context;
    }
    private static String convertStreamToString(InputStream is) {
        /*
         * To convert the InputStream to String we use the BufferedReader.readLine()
         * method. We iterate until the BufferedReader return null which means
         * there's no more data to read. Each line will appended to a StringBuilder
         * and returned as String.
         */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }


    public static JSONObject connect(String url)
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url); 
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream instream = entity.getContent();
                String result= convertStreamToString(instream);
                JSONObject json=new JSONObject(result);
                instream.close();
                return json;
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
    @Override
    public void onPreExecute() {
        progressDialog = new ProgressDialog(curContext);
        progressDialog.setMessage("Loading..Please wait..");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.show();

    }   


 String url ="http://jijio.host22.com/json.php";
     ParsingData client = new ParsingData(this, l);


         client.execute(url);
    }

    GetJSONListener l = new GetJSONListener(){


        @Override
        public void onRemoteCallComplete(JSONObject jsonFromNet) {
            // add code to act on the JSON object that is returned            
            try {
                JSONArray contacts = jsonFromNet.getJSONArray("recipes");
                for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString("id"); 

                    try {
                        if(i==1){
                        String photo = c.getString("image");  
                         System.out.println(c.getString("image"));
                        byte[] encoded = Base64.decode(photo); 
                        setImageViewWithByteArray(image, encoded);
                       System.out.print(encoded);}
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }    
                    System.out.println(id);}

                System.out.print(   jsonFromNet.getString("id"));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

        };

0 个答案:

没有答案