I am trying to create a news viewing part of my final task for graduate studies. What this (UserNewsActivity) does is it fetches data on create by calling my method, like so:
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_news);
InstanciateListViewValues();
}
and fetches data from api I created in Asp .Net Web Api 2 (cause there is also web part in mvc).
Fetched data should be then stored in static var that is in "Global.NewsItemsList", and after that I populate my (custom) ListView with data. Problem is that I am constantly getting error for 200 reponse:
E/Volley: [110] BasicNetwork.performRequest: Unexpected response code 200 for http://192.168.56.1:5000/News/GetNews
I/System.out: NetworkError
And sometimes it just works :S I am 100% sure I am getting properly data (checked in browser, 200 status (OK), check bottom link), and I have used volley to fetch even more data before in project in same way and it works 100% of the time. Method looks like so:
private void InstanciateListViewValues() {
if (Global.NewsItemsList == null) {
final String url = ApiLink + "/News/GetNews";
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Iterator<String> keys = response.keys();
String str_Name = keys.next();
String jsonString = response.optString(str_Name);
Gson gson = new Gson();
Global.NewsItemsList = gson.fromJson(jsonString, new TypeToken<List<NewsDetailsViewModel>>() {
}.getType());
setAdapterData();
} catch (Exception e) {
System.out.println(e.getMessage());
Toast.makeText(UserNewsActivity.this, "An error occured, please try later.",
Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(UserNewsActivity.this, "An error occured, please try later.",
Toast.LENGTH_SHORT).show();
//System.out.println(error.getMessage());//if left uncommented will break app
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
System.out.println("TimeoutError || NoConnectionError");
} else if (error instanceof AuthFailureError) {
System.out.println("AuthFailureError");
} else if (error instanceof ServerError) {
System.out.println("ServerError");
} else if (error instanceof NetworkError) {
System.out.println("NetworkError");
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
JSONObject obj = new JSONObject(res);
System.out.println(obj.toString());
} catch (UnsupportedEncodingException e1) {
System.out.println(e1.getMessage());
e1.printStackTrace();
} catch (JSONException e2) {
System.out.println(e2.getMessage());
e2.printStackTrace();
}
catch (Exception e3) {
System.out.println(e3.getMessage());
e3.printStackTrace();
}
}
} else if (error instanceof ParseError) {
System.out.println("ParseError");
}
}
}
);
queue.add(getRequest);
} else {
setAdapterData();
}
}
If it worth noting I am using VisualStudio to run my services locally, and then using IISExpress to proxy my port, and finally using Genymotion as a emulator.
To keep this question clean, here is link to pastebin, with Global and NewsItemsList classes and response from Web api: https://pastebin.com/5Za6yLDP