0

I am trying to get data from a php file in the internet using JSON. It works in a normal class but will not work when using a fragment.

      public static class MagFragment extends Fragment {
    public static final String ARG_MAGAZINE_NUMBER = "magazine_number";

    public MagFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        if(i != 0){
            rootView = inflater.inflate(R.layout.fragment_magazine, container, false);
            TextView magText = (TextView) rootView.findViewById(R.id.textViewMag);
            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://csddata.site11.com");
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity httpEntity = response.getEntity();
                InputStream inputstream = httpEntity.getContent();
                try{

                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"),8);
                    magText.setText(reader.readLine());

                }catch (Exception e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        }

        return rootView;
    }
}

It works in a normal class so it must just be because it is a fragment.

I dont have a clue hot to copy and paste the errors ... but the only error that had said which line the error was on was the "HttpResponse response = httpclient.execute(httppost);" Line!

Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86
Tom Doe
  • 331
  • 6
  • 23
  • can it be because this class is Static? – Tom Doe May 22 '13 at 12:28
  • If using Eclipse, you can go to the log cat, select the lines that you want to copy, and copy them just as you would anything else. And if that doesn't work, at least re-type the first line of the error... – PearsonArtPhoto Jan 10 '14 at 14:53
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Jave Jan 14 '14 at 17:46

1 Answers1

6

For reference, it previously was extremely common for network operations to be done on the UI thread. When this happens, the response is very sluggish, and often in difficult to predict manners. In order to prevent this from happening, Google prevented users from operating on the UI thread, and instead encourages the use of things like AsyncTask and Threads to get network information, with the data being updated later.

Anytime you have an operation that might take a while, you should operate on a thread, and not on the UI thread. Operating there can cause all sorts of problems. There is a reason that Android made this a run time error, and while it can be annoying sometimes, it should NEVER be done, ESPECIALLY if you EVER intend to release the code. Especially NEVER EVER do network operations on the UI thread!!!!!!

In your case, the proper solution is something like this (Not syntax checked, but should be close...):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    if(i != 0){
        rootView = inflater.inflate(R.layout.fragment_magazine, container, false);
        TextView magText = (TextView) rootView.findViewById(R.id.textViewMag);
        new GetData("http://csddata.site11.com").execute();
    }
}

class GetData extends AsyncTask<String,Void,String>
{
    protected String doInBackground(String... urls) {
            String text="";
            try {

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(urls[0]);
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity httpEntity = response.getEntity();
                InputStream inputstream = httpEntity.getContent();
                try{

                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"),8);
                    text=reader.readLine();

                }catch (Exception e){
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return textl
    }
    @Override
    protect void onPostResults(String result) {
        magText.setText(result);
    }
}
PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • 2
    "Especially NEVER EVER do network operations on the network thread!!!!!!" Do you perhaps mean network operations on the UI thread? – Jave Jan 14 '14 at 17:47