在BackgroundService中使用httpclient.execute时出现RuntimeException

时间:2014-08-19 18:09:55

标签: android service upload multipartentity

            HttpClient httpClient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(urlString);

        HttpResponse response = null;
        MultipartEntity mpEntity = new MultipartEntity();

        StringBody sb;
        String result = null;

        try {
            sb = new StringBody(json.toString());
            mpEntity.addPart("json", sb);

            httppost.setEntity(mpEntity);
            response = httpClient.execute(httppost);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                result = RestClient.convertStreamToString(instream);
                Log.i("Read from Server", result);

            }

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(result == "true"){
            this.stopSelf();
        } else {

        }

我在后台服务中遇到RuntimeException:

response = httpClient.execute(httppost);

我不知道为什么:(我不会在服务中做同样的事情,一切都没问题,但在服务中它会抛出异常。

0 个答案:

没有答案