发送请求有时会落在我的申请表上

时间:2012-12-08 13:56:39

标签: java android http httprequest send

在以下代码中使用我的Android应用程序工作了几个小时后,这个代码崩溃,只是app停止但是如果我没有关闭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;
            }
        }

    }

0 个答案:

没有答案