문제

I was writing an Android application using local server (wamp) and everything worked fine. Now i've put my database online and android started to throw Timeout exception(to any address). I tried to change time out but it did not help.

I can reach the server through a browser.

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

            }

        }



    }
}
도움이 되었습니까?

해결책

May be your local wamp is not configured to load data from remote server.

다른 팁

Increase setConnectTimeout value. or remove the timeconnection once and try to execute maybe it will be helpful to you

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top