我的应用程序将数据发送到服务器并从中获取数据。应用程序在WiFi上成功执行,但在3G上失败。它的例外是网络问题,有时是服务器超时。当我尝试记录它时,它不会给出响应代码。 服务器代码使用REST API准备 这是java代码:
@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;
}
当手机连接到WiFi连接时,代码运行良好。但在3G上它不起作用。当我用手机浏览器打开链接时,它会在3G上成功打开。
即使它没有记录Log.i(TAG, "first_Req_res: " + reponse);
我试图将其用户代理更改为httpURLConnection.setRequestProperty("User-Agent", "");
,但确实改变了任何内容。我还试图增加其连接并读取超时时间。该方法只是不断推出null
。
它的日志看起来像是
................
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
........................
如何才能使其在3G上运行?