My application sends data to server and get data from it. The application successfully executes on WiFi, but it fails on 3G. Its exception is network problem and sometimes server timeout. It does not give response code when I try to log it.
The server code is prepared using REST API.
Here is the java code:
@Override
protected String doInBackground(String... params) {
String lastOduuFk = "new";
String oduuLang = "new";
String ROOT_WEB = "http://www.example.com/";
String updateTaateeUrl = ROOT_WEB + "v1/loadOduu?lastOduuFk=" + lastOduuFk + "&lang=" + oduuLang;
BufferedReader bufferedReader = null;
HttpURLConnection httpURLConnection = null;
URL url = null;
String api_val = "xxxxxxxxxxxxx";
String mainInfo = null;
try {
url = new URL(updateTaateeUrl);
Log.i(TAG, "Try this url");
httpURLConnection = (HttpURLConnection) url.openConnection();
//Property of the connection
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Authorization", api_val);
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/45.0");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setConnectTimeout(30000);
httpURLConnection.setReadTimeout(30000);
httpURLConnection.connect();
int reponse = httpURLConnection.getResponseCode();
Log.i(TAG, "first_Req_res: " + reponse);
InputStream inputStream = httpURLConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line;
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
mainInfo = result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SocketTimeoutException connTimeout) {
this.socketTimedOut = true;
} catch (IOException e) {
e.printStackTrace();
this.netWorkProblem = true;
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return mainInfo;
}
The codes runs well when the phone is connected to WiFi connection. But on 3G it does not work. When I open the link with the phone browser it opens successfully on 3G.
Even it does not log Log.i(TAG, "first_Req_res: " + reponse);
I have tried to change its user-agent to httpURLConnection.setRequestProperty("User-Agent", "");
, but did change any thing. I have also tried to increase its connection and read timeout time. The method just keeps putting out null
.
It log looks like thi
................
08-14 16:17:43.092 14882-16108/xyz.natol.kubbaa I/xyz.natol.kubbaa: Try this url
08-14 16:18:52.420 14882-14882/xyz.natol.kubbaa I/xyz.natol.kubbaa: first_Req_result: null
08-14 16:18:55.923 14882-14882/xyz.natol.kubbaa D/InputMethodManager: windowDismissed mLockisused = false
........................
What shall I do to make it work on 3G?