php - Android总是抛出超时异常

标签 php android mysql timeoutexception

我正在使用本地服务器(wamp)编写一个 Android 应用程序,一切正常。 现在我已经把我的数据库放在网上,android开始抛出超时异常(到任何地址)。我尝试更改超时时间,但没有帮助。

我可以通过浏览器访问服务器。

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);

            }

        }



    }
}

最佳答案

增加 setConnectTimeout 值。或者删除一次时间连接并尝试执行也许会对您有帮助

关于php - Android总是抛出超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118087/

相关文章:

php - 从php和sql中的两个表中提取行

android - 将上下文传递给 SQLiteOpenHelper

java - 使用 fragment 按钮打开另一个 Activity

Android popBackStackImmediate无法移除Glide的SupportRequestManagerFragment

MySQL动态变量被截断

php - SimpleXML:使用包含命名空间的 XML

javascript - 单击按钮 div 显示 1 x 1(一次一个)

php - 如何在 Laravel 中附加多对多关系?

mysql - MySQL中将数据类型varchar转换为数据类型date(一张表)

php while 循环如果对第一行执行其他操作,则对第二行执行操作?