Reputation: 1691
I'm making a request in a web service.
How am I going to test the case where there is no response from server ( cause maybe server is down ) ?
I mean how can I test it on emulator : is there a way I can send a request and block responses to see how my app is handling this case? Also what should I do in such a case?
Upvotes: 0
Views: 686
Reputation: 3
StringRequest stringRequest=new StringRequest(Request.Method.POST,reg_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray=new JSONArray(response);
JSONObject jsonObject=jsonArray.getJSONObject(0);
String message=jsonObject.getString("message"); //message is to be send by server
builder.setTitle("Server Response");
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
Upvotes: 0
Reputation: 61
Consider the following code :
try {
DefaultHttpClient httpclient = getClient();
HttpGet request = new HttpGet();
SchemeRegistry schemeRegistry = httpclient
.getConnectionManager().getSchemeRegistry();
schemeRegistry.register(new Scheme("https",
new TlsSniSocketFactory(), 443));
URI website = new URI(getString(R.string.u3));
request.setURI(website);
HttpResponse response = httpclient.execute(request);
in = new BufferedReader(new InputStreamReader(response
.getEntity().getContent()));
result = in.readLine();
// Log.d(TAG, "Version Check result = " + result);
} catch (Exception e) {
e.printStackTrace();
// Log.e(TAG, "Error in http connection " + e.toString());
}
You need to set a timeout & when the server is down, it will probably go in the Catch block where you can do whatever you want to do when the server is down / you receive no response from the server
Upvotes: 1