url连接getInputStream Android错误

时间:2014-02-23 03:08:54

标签: android inputstream

我正在尝试从Android应用上传服务器。我收到响应代码500的错误,消息是内部服务器错误。 conn.getInputStream将导致异常,其消息是URL。

以下是我的代码,有谁可以帮我弄清楚我在这里缺少什么?

try{
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            try{
                conn.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
                wr.write( data ); 
                wr.flush(); 
                Log.i("GCM","responseCode::"+conn.getResponseCode());
                Log.i("GCM","responseMsg::"+conn.getResponseMessage());
                // Get the server response 
                InputStreamReader in=new InputStreamReader(conn.getInputStream());
                reader = new BufferedReader(in);
                StringBuilder sb = new StringBuilder();
                String line = null;

                // Read Server Response
                while((line = reader.readLine()) != null)
                {
                    Log.i("GCM",line);
                    // Append server response in string
                    sb.append(line + "\n");
                }
                // Append Server Response To Content String 
                Content = sb.toString();
            }
            finally
            {
                conn.disconnect();
                try
                {
                    reader.close();
                }
                catch(Exception ex) {}
            }
        }
        catch(Exception ex)
        {
            Error = ex.getMessage();
            Log.i("GCM","MsgError:::"+Error);
        }

0 个答案:

没有答案