我正在开发一个需要与SQL数据库连接的Android应用程序。该应用程序工作正常,我与服务器连接到一个PHP文件和一个asynk任务和类DefaultHttpClient(见下文)。
然后我用ReadJSONFeedTask读取收到的JSON文件并正确处理数据。一切正常,但当我将两个智能手机与应用程序连接到相同的网络时,其中一个应用程序无法正常工作。
如果我关闭其他智能手机中的wifi连接并通过网络连接3G,这两个应用程序将再次运行。
我尝试过调试,我认为第二个应用程序会产生IOException(见下文),但我不知道如何解决它。
我如何连接:
public class JSONActivity extends Activity {
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpGet httpGet = new HttpGet(URL);
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 7000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 10000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
//HttpClient client = new DefaultHttpClient();
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} else {
Log.e("JSON", "Failed to download file: " + URL);
Toast.makeText(getBaseContext(),"Codigo != 200" + URL, Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"ClientProtocolException " + e.getMessage().toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(),"IOException " + e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
return stringBuilder.toString();
}
//esta no se usa de momento
public class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
JSONArray jsonArray;
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
try {
} catch (Exception e){
e.printStackTrace();
}
}
}
我确实传递了这样的文件:
ReadJSONFeedTask task = new ReadJSONFeedTask();
task.execute('http://www.xxxx.es/archivo.php');
debuger显示下一个错误:
09-19 23:46:37.217: W/System.err(10202): org.apache.http.conn.ConnectTimeoutException: Connect to /213.177.193.111:80 timed out
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-19 23:46:37.217: W/System.err(10202): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-19 23:46:37.225: W/System.err(10202): at com.gestion.LoginActivity.readJSONFeed(LoginActivity.java:3099)
09-19 23:46:37.225: W/System.err(10202): at com.gestion.LoginActivity$CargarArchivo.doInBackground(LoginActivity.java:307)
09-19 23:46:37.225: W/System.err(10202): at com.gestion.LoginActivity$CargarArchivo.doInBackground(LoginActivity.java:1)
09-19 23:46:37.225: W/System.err(10202): at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-19 23:46:37.225: W/System.err(10202): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-19 23:46:37.225: W/System.err(10202): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-19 23:46:37.225: W/System.err(10202): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-19 23:46:37.225: W/System.err(10202): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-19 23:46:37.233: W/System.err(10202): at java.lang.Thread.run(Thread.java:856)
09-19 23:46:37.233: E/JSON(10202): IOException: Connect to /213.177.193.111:80 timed out
感谢给我一个解决这个问题的线索。 此致