Domanda

Io uso questo codice per eseguire una richiesta httpGet al mio server Web.Funziona bene sull'emulatore ma non nel mio senso HTC.L'EXCECUZIONE finisce semplicemente senza fare alcuna richiesta HTTP.Qualche idea?

File f = new File(user.getPhotopath());
List<NameValuePair> reqparams = new LinkedList<NameValuePair>();
reqparams.add(new BasicNameValuePair("email", user.getEmail()));
reqparams.add(new BasicNameValuePair("pwd", user.getPassword()));
reqparams.add(new BasicNameValuePair("name", user.getScreenName()));
reqparams.add(new BasicNameValuePair("photo", f.getName()));
reqparams.add(new BasicNameValuePair("preference", user.getPrefs()));
reqparams.add(new BasicNameValuePair("bluetoothid", bid));

String urlstring = "http://www.mysite.com/me?"+ URLEncodedUtils.format(reqparams, "utf-8");

try {
    URL url = new URL(urlstring);

URI myURI = null;
try {
    myURI = url.toURI();
} catch (URISyntaxException e) {
                e.printStackTrace();
}
HttpClient httpClient = new DefaultHttpClient();
HttpGet getMethod = new HttpGet(myURI);
HttpResponse webServerResponse = null;
HttpEntity httpEntity = null;
try {
    webServerResponse = httpClient.execute(getMethod);
    httpEntity = webServerResponse.getEntity();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
if (httpEntity != null) {

    InputStream instream = httpEntity.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 {
            resultStr = sb.toString();
            instream.close();
        } catch (IOException e) {
                e.printStackTrace();
        }
    }
.

È stato utile?

Soluzione

Mentre il tuo approccio è corretto e funzionerà, è molto più facile usare il EntityUtils classe helper per recuperare il corpo di risposta come una stringa.Semplicemente:

String body = EntityUtils.toString(httpEntity);
.

Altri suggerimenti

Non c'era eccezione né avvertimento nello stacktrace.Solo messaggi di allocazione della memoria.Ad ogni modo ho risolto utilizzando urlconnection anziché httpclient:

try {
      URL url = new URL(urlstring);
      URLConnection connection = url.openConnection();
      InputStream inputStream = connection.getInputStream();
      BufferedInputStream bufferedInput = new BufferedInputStream(inputStream);

    // Read the response into a byte array
      ByteArrayBuffer byteArray = new ByteArrayBuffer(50);
      int current = 0;
      while((current = bufferedInput.read()) != -1){
            byteArray.append((byte)current);
      }

      // Construct a String object from the byte array containing the response
     resultStr = new String(byteArray.toByteArray());
    } catch (Exception e) {
      e.printStackTrace();
    }
.

per qualche motivo ha funzionato.Sto ancora cercando di capire cosa è andato storto.

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