Pregunta

sorry for my English. I have this problem: When I work with httpclient, I don`t get full message from server. This is my code:

                  DefaultHttpClient hc = new DefaultHttpClient();
                  HttpPost postMethod = new HttpPost(params[0]);

                  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.length-1);
                  for(int i=1;i<params.length;i++){
                    int endF=params[i].indexOf("=");
                    nameValuePairs.add(new BasicNameValuePair(params[i].substring(0, endF),
                            params[i].substring(endF+1, params[i].length())));
                  }

                  // Url Encoding the POST parameters
                  try {
                      postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                  } catch (UnsupportedEncodingException e) {
                      // writing error to Log
                      e.printStackTrace();
                  }



                try {

                    HttpResponse resp = hc.execute(postMethod);
                    BufferedReader in = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));

                    StringBuffer sbuffer = new StringBuffer("");
                    String line = "";

                    while ((line = in.readLine()) != null) {
                        sbuffer.append(line + "\n");
                    }
                    in.close();

                    response = sbuffer.toString();

And JSON, that I get, even doesn't close by "}". Simply It was cut. Where next part? and how I can get it?

¿Fue útil?

Solución

Try to do it that way, if it doesn't work, you have probably some error in the server side.

final HttpClient httpclient = new DefaultHttpClient();
final HttpPost httppost = new HttpPost("http://www.yourdomain.com/service.php");

try {
  // ... Leave the post send part untouched ...

  InputStream ips  = response.getEntity().getContent();
  BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
  if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
    try { throw new Exception(response.getStatusLine().getReasonPhrase()); } 
    catch (Exception e) { e.printStackTrace(); }
  }

  String s;
  StringBuilder sb = new StringBuilder();

  while (true) {
    s = buf.readLine();
    if (s == null || s.length() == 0)
      break;
    sb.append(s);
  }

  buf.close();
  ips.close();

  Log.d("yourTag", "Post response: " + sb.toString());
} 
catch (ClientProtocolException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top