Autore Topic: app che si connette ad un local host  (Letto 1144 volte)

Offline AliceInWonderland

  • Nuovo arrivato
  • *
  • Post: 23
  • Respect: +1
    • Github
    • Google+
    • Raffaella Pivetta
    • pivetta.raffaella@hotmail.it
    • Mostra profilo
  • Dispositivo Android:
    samsung galaxy nexus ( android 4.3 )
  • Sistema operativo:
    window 7
app che si connette ad un local host
« il: 02 Maggio 2012, 16:48:43 CEST »
0
 ;-) Cia a tutti sto scrivendo un'applicazione che si connette ad un server web e una volta stabilita la connessione gli invia dati salvati sul db del dispositivo...
ora non riesco a capire perchè non funzioni:
Codice (Java): [Seleziona]
/**
 * AsyncTask enables proper and easy use of the UI thread. This class allows to perform background
 * operations and publish results on the UI thread without having to manipulate threads and/or handlers**/

public class NetworkTask extends AsyncTask<String, Void,  JSONArray>{

        /**Runs on the UI thread before doInBackground(Params...).**/
        @Override
        protected void onPreExecute() {
                  Log.i("AsyncTask", "onPreExecute");
        }

       
        /**Override this method to perform a computation on a background thread*/
        @Override
        protected JSONArray doInBackground(String... params) {
                try {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://localhost/ciao.php");
                       
                        // Add your data
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                        nameValuePairs.add(new BasicNameValuePair("stringdata", "Connection "));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                       
                         // Execute HTTP Post Request
                         HttpResponse response = httpclient.execute(httppost);
                         BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "iso-8859-1"), 8);
                         StringBuilder sb = new StringBuilder();
                         sb.append(reader.readLine() + "\n");
                 String line = "0";
                 while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
                    }
                 reader.close();
                 String result11 = sb.toString();

                 // parsing data
                 return new JSONArray(result11);
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                         return null;
                } catch (ClientProtocolException e) {
                        e.printStackTrace();
                         return null;
                } catch (IOException e) {
                        e.printStackTrace();
                         return null;
                } catch (JSONException e) {
                        e.printStackTrace();
                        return null;
                }
        }
       
        /**Runs on the UI thread after doInBackground(Params...). The specified result is the value returned
         *  by doInBackground(Params...).This method won't be invoked if the task was cancelled.**/

        @Override
        protected void onPostExecute(JSONArray result) {
                 if (result != null) {
                         Log.i("AsyncTask", "onPostExecute: Completed.");
                } else {
                    // error occured
                        Log.i("AsyncTask", "onPostExecute: Completed with an Error.");
                               

                }
        }

questo è il log cat
Codice: [Seleziona]
05-02 16:45:07.521: W/System.err(15317): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost refused
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-02 16:45:07.537: W/System.err(15317):         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-02 16:45:07.537: W/System.err(15317):         at sogetel.pivetta.CapturEvent.asincTask.NetworkTask.doInBackground(NetworkTask.java:51)
05-02 16:45:07.537: W/System.err(15317):         at sogetel.pivetta.CapturEvent.asincTask.NetworkTask.doInBackground(NetworkTask.java:1)
05-02 16:45:07.537: W/System.err(15317):         at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-02 16:45:07.537: W/System.err(15317):         at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-02 16:45:07.537: W/System.err(15317):         at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-02 16:45:07.544: W/System.err(15317):         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-02 16:45:07.544: W/System.err(15317):         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-02 16:45:07.544: W/System.err(15317):         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-02 16:45:07.544: W/System.err(15317):         at java.lang.Thread.run(Thread.java:856)
05-02 16:45:07.560: W/System.err(15317): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
05-02 16:45:07.568: W/System.err(15317):         at libcore.io.IoBridge.connect(IoBridge.java:114)
05-02 16:45:07.568: W/System.err(15317):         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-02 16:45:07.568: W/System.err(15317):         at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-02 16:45:07.568: W/System.err(15317):         at java.net.Socket.connect(Socket.java:842)
05-02 16:45:07.576: W/System.err(15317):         at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-02 16:45:07.576: W/System.err(15317):         at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-02 16:45:07.576: W/System.err(15317):         ... 15 more
05-02 16:45:07.576: W/System.err(15317): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
05-02 16:45:07.584: W/System.err(15317):         at libcore.io.Posix.connect(Native Method)
05-02 16:45:07.584: W/System.err(15317):         at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-02 16:45:07.584: W/System.err(15317):         at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-02 16:45:07.584: W/System.err(15317):         at libcore.io.IoBridge.connect(IoBridge.java:112)
05-02 16:45:07.584: W/System.err(15317):         ... 20 more


la riga che genera l'errore è la seguente:
Codice (Java): [Seleziona]
 HttpResponse response = httpclient.execute(httppost);
help please non capisco perchè non funzioni
Raffaella

Offline bradipao

  • Moderatore globale
  • Utente storico
  • *****
  • Post: 4043
  • keep it simple
  • Respect: +567
    • Github
    • Google+
    • bradipao
    • Mostra profilo
  • Dispositivo Android:
    Nexus 5
  • Play Store ID:
    Bradipao
  • Sistema operativo:
    W7
Re:app che si connette ad un local host
« Risposta #1 il: 02 Maggio 2012, 17:02:50 CEST »
+1
Se ho ben interpretato, con localhost tu vorresti collegarti alla macchina su cui stai facendo girare l'emulatore.

In tal caso, localhost non è il la macchina locale, devi mettere 10.0.2.2 al posto di localhost.

Using the Android Emulator | Android Developers


Se intendevi altro, come non detto.
NON rispondo a domande nei messaggi privati
Bradipao @ Play Store

Offline AliceInWonderland

  • Nuovo arrivato
  • *
  • Post: 23
  • Respect: +1
    • Github
    • Google+
    • Raffaella Pivetta
    • pivetta.raffaella@hotmail.it
    • Mostra profilo
  • Dispositivo Android:
    samsung galaxy nexus ( android 4.3 )
  • Sistema operativo:
    window 7
Re:app che si connette ad un local host
« Risposta #2 il: 03 Maggio 2012, 10:23:51 CEST »
0
aah ora ho capito  :-o o_O ecco perchè non funzionava!!!!  ;-)  :D grazie   O:-)
Raffaella