I am trying to send data on server using JsonParse class. Now when I try to make a request to the server it always give SocketTimeOut Exception, but on the server side data is being uploaded always. This means data is uploaded successfully but I did not get response on client side. I have check my link from browser and getting complete response.
I my Login scenario user get login on server but I did not get user data in response.
NOTE:
Actually this problem is happening when wifi or data connection is connected but speed is slow/too slow.
I have search on this issue but did not find satisfactory solution.
This is class JsonParse (partly based on this answer by kuester2000)
public class JSONParser {
InputStream is = null;
JSONObject jObj = null;
String json = "";
int timeoutConnection = 3000;
JSONObject jsonObject = new JSONObject();
JSONObject mainJson = new JSONObject();
BufferedReader reader;
Context context;
// constructor
public JSONParser(Context context) {
this.context = context;
}
// function get json from url
// by making HTTP POST or GET mehtod
public String makeHttpRequest(String url, String method,
List<NameValuePair> params) {
//mobiledataenable(true);
is = null;
jObj = null;
json = "";
timeoutConnection = 3000;
jsonObject = new JSONObject();
mainJson = new JSONObject();
// Making HTTP request
try {
// check for request method
//if(method == "POST")
if (method.equals("POST")) {
// request method is POST
// defaultHttpClient
Log.e("JSONParser", "POST METHOD ");
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 15000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
//Log.e("1111.........",",,,,,,,,"+EntityUtils.toString(httpEntity) );
is = httpEntity.getContent();
Log.e("JSONParser is", is.toString());
return bufferMethod();
} else if (method == "GET") {
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 10000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 15000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
String paramString = URLEncodedUtils.format(params, "utf-8");
if (params.size() != 0)
url += "?" + paramString;
Log.e("JSONParser", "GET METHOD..... " + url);
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
// Log.e("2222*************.........", ",,,,,,,," + httpResponse.getEntity().getContent().toString());
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.e("JSONParser is", is.toString());
return bufferMethod();
}
} catch (SocketTimeoutException e) {
// Toast.makeText(getApplicationContext(), "Server timeout", Toast.LENGTH_LONG).show();
Log.e("SOCK TIMEOUT", e.toString());
jObj = null;
json = new String();
json = "5";
return json;
} catch (ConnectTimeoutException e) {
e.printStackTrace();
jObj = null;
json = new String();
return json;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("UnsupportedEncodingException", e.toString());
jObj = null;
json = new String();
return json;
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("ClientProtocolException", e.toString());
jObj = null;
json = new String();
return json;
} catch (IOException e) {
e.printStackTrace();
Log.e("IOException", e.toString());
jObj = null;
json = new String();
return json;
}
return bufferMethod();
}
public String bufferMethod() {
try {
reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
reader.close();
json = new String();
json = sb.toString();
Log.e("json to string----", "...." + json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
jObj = null;
json = new String();
if (e.toString().equalsIgnoreCase("java.net.SocketTimeoutException")) {
json = "5";
Log.e("222222----", "...." + json);
} else {
json = "6";
Log.e("333333----", "...." + json);
}
return json;
}
return json;
}
}
and this is the request from MainAcivity
public class Logout extends AsyncTask<String, String, String> {
int response = 0;
private ProgressDialog pDialog;
String transport_route = "";
String _number_of_vehicle = "";
private Exception mException;
Logout asyncObject;
private ArrayList<HashMap<String, Object>> TemporaryUploadBackUp = new ArrayList<HashMap<String, Object>>();
String returnCode = new String();
String json_string = new String();
HttpResponse responsee;
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
asyncObject = this;
pDialog = new ProgressDialog(RouteTickiting.this);
pDialog.setMessage("Please wait....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
returnCode = new String();
}
/**
* getting All BusInfo from url
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
JSONParser jParser = new JSONParser(RouteTickiting.this);
jParser = null;
jParser = new JSONParser(RouteTickiting.this);
JSONObject jsonReturn = new JSONObject();
jsonReturn = null;
returnCode = new String();
json_string = new String();
json_string = jParser.makeHttpRequest("some link"?"
+ "user_email=" + user_email +
"&user_password=" + user_password +
, "POST", nameValuePairs);
Log.e("loging json data------", "....." + json_string);
if (json_string.length() == 1) {
if (json_string.equalsIgnoreCase("5")) {
returnCode = "5";
finish();
session.logoutUser();
} else if (json_string.equalsIgnoreCase("6")) {
returnCode = "6";
}
} else {
try {
jsonReturn = new JSONObject(json_string);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
if (jsonReturn != null) {
try {
Log.e("----logout user......", "..." + jsonReturn);
JSONArray error_code = jsonReturn.getJSONArray("result");
JSONObject jsonObject = error_code.getJSONObject(0);
if (jsonObject.getString("bool").equalsIgnoreCase("1")) {
//success
returnCode = "1";
finish();
session.logoutUser();
} else if (jsonObject.getString("bool").equalsIgnoreCase("0")) {
//Already login
returnCode = "0";
finish();
session.logoutUser();
} else if (jsonObject.getString("bool").equalsIgnoreCase("2")) {
//Unable to login
returnCode = "2";
} else if (jsonObject.getString("bool").equalsIgnoreCase("3")) {
//USer does not exist
returnCode = "3";
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
if (pDialog != null)
if (pDialog.isShowing())
pDialog.dismiss();
if (returnCode.equalsIgnoreCase("6")) {
customToast("try again");
}
}
}