我正在使用本地服务器(wamp)编写一个 Android 应用程序,一切正常。现在我已经把我的数据库放到网上,android 开始抛出 Timeout 异常(到任何地址)。我试图改变时间,但它没有帮助。
我可以通过浏览器访问服务器。
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;
public class MainActivity extends Activity {
JSONArray json1;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=(TextView) findViewById(R.id.text);
test ts=new test(getApplicationContext());
ts.setcityspinner();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class test {
Context cnt;
public test(Context c) {
// TODO Auto-generated constructor stub
cnt=c;
}
public void setcityspinner()
{
//spin=sp;
LoadList ll=new LoadList();
ll.execute("http://smartmope.kz/php/get_all_cities.php");
}
public class LoadList extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
URL u = new URL(params[0]);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(20000);
c.setReadTimeout(20000);
c.connect();
int status = c.getResponseCode();
publishProgress(" "+status);
// Toast.makeText(cnt, " "+status, Toast.LENGTH_LONG).show();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
publishProgress(sb.toString());
return sb.toString();
}
} catch (MalformedURLException ex) {
//txt.setText(ex.toString());
Log.d("mylog", ex.toString());
publishProgress(ex.toString());
//Toast.makeText(cnt, ex.toString(), Toast.LENGTH_LONG).show();
} catch (IOException ex) {
// txt.setText(ex.toString());
publishProgress(ex.toString());
Log.d("mylog", ex.toString());
// Toast.makeText(cnt, ex.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
Toast.makeText(cnt, values[0], Toast.LENGTH_LONG).show();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// Toast.makeText(cnt, result, Toast.LENGTH_LONG).show();
if (result!=null){
txt.setText(result.toString());
}
super.onPostExecute(result);
}
}
}
}