为什么android中没有异常生成

时间:2014-12-20 05:45:46

标签: android

我有这个代码,它不会产生任何错误或异常,同时它也不起作用。什么都不做,坐在那里。我无法理解为什么或甚至发生任何错误。

//Method to post data to webservice

public void post() throws UnsupportedEncodingException
{
    try {
        Toast.makeText(getBaseContext(), "Creating new user", Toast.LENGTH_SHORT).show();
        HttpURLConnection urlConnection = (HttpURLConnection) (new URL("http://www.rgbpallete.in/led/api/signup").openConnection());
        urlConnection.setConnectTimeout(1500);
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("uname", uname));
        params.add(new BasicNameValuePair("pass", password));
        params.add(new BasicNameValuePair("email", email));

        OutputStream os = urlConnection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getQuery(params));
        writer.flush();
        writer.close();
        os.close();

        urlConnection.connect();
        if(urlConnection.getResponseCode() == 200){
            InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);
            JSONObject json = new JSONObject(responseStrBuilder.toString());
            String message = json.getString("message");
            boolean error = json.getBoolean("error");

            error(error, message);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

此代码在通过我的设备执行时不执行任何操作,并且在调试时我发现它已在行OutputStream os = urlConnection.getOutputStream();上消失

为什么它不会产生任何异常,即使它在try catch块中并且如果没有异常或错误那么为什么它不起作用?

0 个答案:

没有答案