我是Android和编程的新手。 我已经构建了一个运行服务的活动,该服务连接到PHP脚本以从服务器提取一些数据。 问题是,当程序无法访问服务器时,它会崩溃。 每次无法访问php脚本时,如何防止程序崩溃? 感谢。
代码的相关部分:
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(returnString);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader 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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
return result;
}
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): <p>The requested URL /getAvgScore1.php was not found on this server.</p>
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): <p>Additionally, a 404 Not Found
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): error was encountered while trying to use an ErrorDocument to handle the request.</p>
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): </body></html>
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:132)
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:310)
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): at java.lang.Float.parseFloat(Float.java:327)
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): at java.lang.Float.valueOf(Float.java:368)
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): at iAndroid.RYCQ.rankservice.onCreate(rankservice.java:46)
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2465)
08-29 13:10:01.571: ERROR/AndroidRuntime(1116): ... 10 more
08-29 13:10:01.581: INFO/Process(583): Sending signal. PID: 1116 SIG: 3
08-29 13:10:01.581: INFO/dalvikvm(1116): threadid=7: reacting to signal 3
08-29 13:10:01.591: INFO/dalvikvm(1116): Wrote stack trace to '/data/anr/traces.txt'
08-29 13:10:10.634: WARN/ActivityManager(583): Launch timeout has expired, giving up wake lock!
08-29 13:10:10.634: WARN/ActivityManager(583): Activity idle timeout for HistoryRecord{436eed60 {iAndroid.RYCQ/iAndroid.RYCQ.finalresult}}
08-29 13:10:20.698: WARN/ActivityManager(583): Timeout executing service: ServiceRecord{437c8d88 iAndroid.RYCQ/.rankservice}
答案 0 :(得分:1)
当连接到服务器失败时,可能只需要捕获所引发的异常。您需要发布您当前使用的代码。如果不能看到你做了什么,我们只能随意猜测它会解决什么问题。
答案 1 :(得分:0)
public static InputStream fetch(String urlString) throws MalformedURLException, IOException {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
您必须输入timeoutConnection和timeoutSocket值,以便设备不会永远等待响应。有关字段的更多信息可访问here。
答案 2 :(得分:0)
如果第一个try
块中引发了异常,则会在BufferedReader
null
上实例化InputStream
。只有一个try/catch
块,或者在第一个catch
中正确退出该方法。