Domanda

Ho scritto una logica che chiama un webservice Android passa pochi paramters. Il problema è quando i inviare interrogazione restituisce un messaggio di errore che mi sto ricevendo come XML. l'url che voglio chiamata è http://192.168.1.10:8080/ymaws/resources/restaurantcityid=33498&areanm=vasant vihar ma io m Come error.The codice è qui sotto. Plz suggerire un buon modo per fare questo

String list = null;
                restaurantnames=new ArrayList<String> ();   
                areanames=new ArrayList<String>();
                restaurantidlist=new ArrayList<String>();

                final HttpClient client=new DefaultHttpClient();
                                    String url = "http://192.168.1.10:8080/ymaws/resources/restaurant?cityid="+cityid+"&areanm="+area.getSelectedItem().toString();
                String encodedurl = null;
                try
                {
                        encodedurl = URLEncoder.encode(url,"UTF-8");
                } 
                catch (UnsupportedEncodingException e1) 
                {
                        e1.printStackTrace();
                }
                Log.i("TEST", encodedurl);

                final HttpGet req=new HttpGet(encodedurl);
                HttpResponse httpResponse;
                try {
                        httpResponse=client.execute(req);
                        HttpEntity entity = httpResponse.getEntity();
                        Log.i("entity", entity.toString());
                        if (entity != null) 
                        {
                            InputStream instream = entity.getContent();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                            StringBuilder sb = new StringBuilder();

                            String line = null;
                            try 
                            {
                                while ((line = reader.readLine()) != null) 
                                {
                                    sb.append(line + "\n");
                                }
                            } 
                            catch (IOException e) 
                            {
                                e.printStackTrace();
                            } 
                            finally 
                            {
                                try 
                                {
                                    instream.close();
                                }
                                catch (IOException e)
                                {
                                    e.printStackTrace();
                                }
                            }

                            // Closing the input stream will trigger connection release
                            list= sb.toString();
                            Log.i("list xml is", list.toString());
È stato utile?

Soluzione

Prova semplicemente aggiungendo "amp;" dopo la "&" segno. (Non poteva scrivere e + amp; insieme dal SO fatto al solo "&" hehe) :)

Altri suggerimenti

Questo potrebbe richiedere u utilizzare librerie opensource di Apache, ma funziona per me in tutto il mio codice:

public static String getHTTP(String url) throws ClientProtocolException, IOException {
        String result = "";     
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();           

        // Execute HTTP Get Request
        result = httpclient.execute(httpget, responseHandler);
        return result;
}

Le librerie richieste sono:

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.ClientProtocolException;
import java.io.IOException;

È possibile ottenere il client HTTP Apache vasetti qui

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top