0

在使用我的 android 应用程序工作几个小时后,在下面的代码中,此代码崩溃,只是应用程序停止了,但如果我不关闭 wifi 连接工作,如果停止并尝试让一些资源应用程序崩溃。

public String getRequest(String url, ArrayList<NameValuePair> nameValuePairs)
    {

        HttpParams httpParameters = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
        HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);

        AndroidHttpClient httpclient = null;

        //HttpClient httpclient = null;
        try {

            httpclient  = AndroidHttpClient.newInstance("V");


            //  httpclient = new DefaultHttpClient(httpParameters);

            httpclient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);


            HttpPost httppost = new HttpPost(url);

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));  

            HttpResponse response = httpclient.execute(httppost);

            if((response != null) && (response.getStatusLine().getStatusCode() != 200))
            {
                int code =  response.getStatusLine().getStatusCode();
                httpclient.close();
                throw new IOException("Bad status code! - "+ code);
            }

            HttpEntity entity = response.getEntity();
            InputStream instream = entity.getContent();

            Reader reader = new InputStreamReader(instream, HTTP.UTF_8);

            StringBuilder buffer = new StringBuilder("");

            char[] tmp = new char[1024];
            int l;
            while ((l = reader.read(tmp)) != -1) {
                buffer.append(tmp, 0, l);
            }

            reader.close();

            try{
                if(entity != null){
                    entity.consumeContent();
                }
            }catch(Exception e){
                if((e != null) && (e.getMessage() != null)){
                    Log.e("V","Consume content ex-> "+e.getMessage().toString());
                }

            }


            String resp = buffer.toString().replace("&quot;", "\"");

            Log.d("V", "RESPONSE:-----\n" + resp + "\n--------------");

            try{

            httpclient.close();

            } catch(Exception e_close){
                if((e_close != null) && (e_close.getMessage() != null)){
                    Log.e("V","Exception in closing"+e_close.getMessage().toString());
                }
                Log.e("V","Exception - closing http unsuccessfully!");
            }

            return resp;

        } catch (IOException e){

            Log.e("V IOException [Send Request]","IO Ex");
            if((e != null) && (e.getMessage() != null)){
                Log.e("V",e.getMessage().toString());
            }
            return "";


        } catch (Exception e) { 

            Log.e("V Exception [Send Request]","Exception Requester");
            if((e != null) && (e.getMessage() != null)){
                Log.e("V",e.getMessage().toString());
            }
            return "";

        }finally {
            if (httpclient != null && httpclient.getConnectionManager() != null) {
                httpclient.getConnectionManager().shutdown();
                httpclient = null;
            }
        }

    }
4

0 回答 0