上传图像到服务器时错误消息访问被拒绝

时间:2011-10-17 05:03:22

标签: android image upload

 I am new to android. So, not able to trace out the error message while uploading image to the server. 

我从互联网上获得了一些代码,用于将图像上传到服务器和link is并相应地更改了我的程序。当我尝试运行该程序时,它会在模拟器中显示错误消息,如下面的屏幕截图所示。enter image description here

甚至检查了本地主机中的所有内容,它运行正常。这是我上传图片的代码。请检查一下并给我一些建议。

public class uploadimage extends Activity {

    InputStream inputStream;

        @Override

    public void onCreate(Bundle icicle) {

            super.onCreate(icicle);

            setContentView(R.layout.main);



            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);           ByteArrayOutputStream stream = new ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.

            byte [] byte_arr = stream.toByteArray();

            String image_str = Base64.encodeBytes(byte_arr);

            ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();



            nameValuePairs.add(new BasicNameValuePair("image",image_str));



            try{

                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost("http://localhost/Upload_image_ANDROID/upload_image.php");

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httppost);

                String the_string_response = convertResponseToString(response);

                Toast.makeText(uploadimage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();

            }catch(Exception e){

                  Toast.makeText(uploadimage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();

                  System.out.println("Error in http connection "+e.toString());

            }

        }


        public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{



             String res = "";

             StringBuffer buffer = new StringBuffer();

             inputStream = response.getEntity().getContent();

             int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..

             Toast.makeText(uploadimage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();

             if (contentLength < 0){

             }

             else{

                    byte[] data = new byte[512];

                    int len = 0;

                    try
                    {

                        while (-1 != (len = inputStream.read(data)) )

                        {

                            buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..

                        }

                    }

                    catch (IOException e)

                    {

                        e.printStackTrace();

                    }

                    try

                    {

                        inputStream.close(); // closing the stream…..

                    }

                    catch (IOException e)

                    {

                        e.printStackTrace();

                    }

                    res = buffer.toString();     // converting stringbuffer to string…..



                    Toast.makeText(uploadimage.this, "Result : " + res, Toast.LENGTH_LONG).show();

                    //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));

             }

             return res;

        }

}

提前致谢。

1 个答案:

答案 0 :(得分:2)

而不是

"http://localhost"

使用

http://10.0.2.2

用于模拟器。如果您想测试设备,请使用路由器或网络提供的specific IP of System

Thankx。

相关问题