HttpPost适用于Android的SDK 8,但不适用于SDK 17

时间:2014-08-12 17:15:12

标签: java android json http sdk

当我将SDK 17插入清单文件时,没有任何作用(它不会加载)。但是当我插入SDK 8时,它可以工作。

private void loadq(){
        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost("http://iwindroids.ru/app/fla/getq");
        httppost.setHeader("Content-type", "application/json");
        InputStream inputStream = null;String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);           
            HttpEntity entity = response.getEntity();
            inputStream = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null){sb.append(line + "\n");}
            result = sb.toString();
            JSONObject jObject = new JSONObject(result);
            String t = jObject.getString("t").replace("--", "—");
            String a = jObject.getString("a");
            //String i = jObject.getString("i");
            TextView et = (TextView) findViewById(R.id.textView1);
            EditText at = (EditText) findViewById(R.id.editText2);
            et.setText(t+".");
            at.setText(a);
            live();
            showTimer();
        } catch (Exception e){}
        finally {try{if(inputStream != null)inputStream.close();}catch(Exception squish){}}
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

你有NetworkOnMainThreadException。查看LogCat以查看它。您必须将代码放在AsyncTask或线程中。