我正在开发一个需要与 SQL 数据库连接的 Android 应用程序。该应用程序运行良好,我将服务器连接到 php 文件并使用 async 任务和 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');
调试器显示下一个错误:
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
谢谢你给我一个解决这个问题的线索。问候