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