2

Im having trouble with a piece of code in android studio that has been working reliably for weeks. Anytime i try to receive Json via localhost (for debugging in the emulator or on my device and connecting over wifi) i get the error message:

com.google.gson.JsonParseException: unable to parse json

however if i connect to a remote server for a JSON Echo such as:

http://echo.jsontest.com/insert-key-here/insert-value-here/key/value

there is no problem.

I have verified that the JSON is valid, and like i say its worked previously for me but I've double checked it online anyway, i even created a new bare bones app with just a button and a request but still have the same issue.

The code is as follows:

    public static class PlaceholderFragment extends Fragment {

    public static final String ACCOUNT_VIEW_URL = "http://192.168.0.10/~user/tests/accountEcho.php";  //"http://echo.jsontest.com/insert-key-here/insert-value-here/key/value";
    Button button;

    private static final String ARG_SECTION_NUMBER = "section_number";


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false);
        button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netWork();
            }
        });
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((HomeScreenActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }


    void netWork() {
        Ion.with(getActivity())
                .load(ACCOUNT_VIEW_URL)
                .asJsonObject()
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {

                        if (null == e) {
                            //   Log.v("onCompleted", "not null");
                            Log.v("result", ""+result+"");
                        } else {
                            //  Log.v("onCompleted", "null");
                            Log.v("on completed", "" + e + "");

                        }
                        //TODO:::: JSON STRUCTURE - LOAD DP INTO VIEW
                    }
                });

    }
}

}

and the JSON response is:

{
        "accType": "2",
        "firstname": "duracell",
        "lastname": "battery"
        "house_num": "122",
        "addL1": "Brick Lane",
        "addL2": "",
        "city": "Middx"

}

Also if i visit the same localhost address from any other application on my computer (visualJSON, chromeAdvancedRestClient, safari, firefox etc..) it works fine and the response is as expected.

Can anyone advise me whats going on??

Thanks

vitamike
  • 123
  • 1
  • 15

2 Answers2

0

If your JSON is on a server and you access it through an HTTP request, you need to parse only the content, not the whole file with the headers.

Try doing an HTTP client, use getResponse.getEntity or getResponse.getContent, and then your parser on one of these.

I copy that from here.

Community
  • 1
  • 1
Rectangle
  • 5
  • 1
0

This is how i tackle that :

STEP 1:

Make sure you have an object class , the objects have to having the same names with your JSON objects.

public class Tester {

    private String accType;
    private String firstname;
    private String lastname;
    private String addL1;
    private String addL2;
    private String city;


    public String getAccType() {
        return accType;
    }

    public void setAccType(String accType) {
        this.accType = accType;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getAddL1() {
        return addL1;
    }

    public void setAddL1(String addL1) {
        this.addL1 = addL1;
    }

    public String getAddL2() {
        return addL2;
    }

    public void setAddL2(String addL2) {
        this.addL2 = addL2;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

STEP 2:

Then i retrieve the JSON data as a list , but if suppose , i have one item , i just index each of the elements as below

        Ion.with(getBaseContext())
                        .load(url_)
                        .progressDialog(pd)
                        .as(new TypeToken<List<Tester>>() {
                        })
                        .setCallback(new FutureCallback<List<Tester>>() {

                            @Override
                            public void onCompleted(Exception e, List<Tester> itemList) {

                                try {


                                    String firstname= itemList.get(0).getFirstname();

//DO the same to other elements

      Log.v("result", "Firstname :"+firstname+"");


                                    pd.dismiss();


                                }

                                catch (Exception ex){

                                    Toast.makeText(getApplicationContext(),"Please check your Internet connection",Toast.LENGTH_LONG).show();
                                }






                            }



                        });


        }
Lutaaya Huzaifah Idris
  • 3,596
  • 8
  • 38
  • 77