android中的连接问题,流返回null

时间:2014-02-06 04:51:58

标签: android json connection

我正在尝试从databaselistview获取数据。我已经使用JSON解析。当我运行servlet时,我得到正确的解析值。现在我试图建立连接,但它没有关闭

DBmapping

public class mySearch extends Activity {

    class Event_location{
        String Place;
        String Area;
        int cost2;
        int cost2wa;
        int uid;
        @Override
        public String toString() {
            return "Venue details [Place=" + Place +", Area=" + Area + ", cost2=" + cost2 + ", cost2wa = "+ cost2wa +"]";
        }
    }
    class FetchCollegeDetailsTask extends AsyncTask<Void, Void, InputStream> {

        @Override
        protected InputStream doInBackground(Void... arg0) {
            Log.e("TESTING @@@@@@" ,"TEST FOR CONNNECTION" +
                    "" );
            //192.168.1.102:8080/First/MyServlet
            //String stringURL="http://www.google.com";
            String stringUrl="http://192.168.1.102:8080/First/MyServlet";
            URL url;
            try {
                url = new URL(stringUrl);

                    Log.e("URL DATA TEST  "   , ""+ url);
                //Log.e("test"+ url.openConnection().getInputStream() , "");
                    Log.e("Hiii", "hiii");

                    //Log.e("Input",""+url.openConnection().getInputStream() );
                //  return url.openConnection().getInputStream();
                URLConnection connection = url.openConnection();
                Log.e("TEST" ,""+ connection);

在上面的行之后没有发生任何事情::

            InputStream stream = connection.getInputStream();
                Log.e("STREAM" ,""+ stream);
                //String test = url.openConnection().getInputStream();
                return stream;


            } catch (MalformedURLException e) {

                Log.e("Amit", "search");
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            return null;
        }

        @Override
        protected void onPostExecute(InputStream result) {
            super.onPostExecute(result);

            Log.e("Result", ""+result);
            StringBuilder builder = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(result));

            String line = null;
            try {
                while((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            String jsonString = builder.toString();
            Log.e("GetCoursesActivity", jsonString);

            try {
                JSONObject rootObject = new JSONObject(jsonString);
                JSONArray jsonArray = rootObject.getJSONArray("event_locations");               
                for (int index = 0; index < jsonArray.length(); index++) {
                    JSONObject object = (JSONObject) jsonArray.get(index);
                    Event_location detail = new Event_location();
                    detail.Place = object.getString("Place");
                    detail.Area = object.getString("Area");
                    detail.cost2 = object.getInt("Cost_for_2");
                    detail.cost2wa = object.getInt("Cost_for_2_WA");
                    array.add(detail);
                }
            }

                catch (JSONException e) {
                    e.printStackTrace();
                }
            adapter.notifyDataSetChanged();
            }

}
    private ArrayList<Event_location> array;
    private ArrayAdapter<Event_location> adapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);


        array = new ArrayList<Event_location>();
        Log.e("Array"+ array ,"");

        listView = (ListView) findViewById(R.id.listView);
        adapter = new ArrayAdapter<Event_location>(this, android.R.layout.simple_expandable_list_item_1,array);
      //  adapter = new ArrayAdapter<Event_location>(this, android.R.layout.simple_list_item_1, array);
        listView.setAdapter(adapter);
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.e("Test","Test");

            if (item.getTitle().equals("Parse")) {
                StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectNetwork().detectAll().build());
                new FetchCollegeDetailsTask().execute();
            }
            return super.onOptionsItemSelected(item);
    }
单击菜单

中的解析选项后,

数据应显示在listview

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            menu.add("Parse");
        return true;
    }
}

0 个答案:

没有答案