【问题标题】:Why isn't the server returning anything?为什么服务器没有返回任何东西?
【发布时间】:2023-04-10 23:50:01
【问题描述】:

我使用以下代码从 php 页面 (www.ace.ucv.ro/android/android.php) 中检索基本上 JSON 格式的字符串。

出于某种原因,无论我尝试什么,字符串“result”仍然为空,其中没有存储任何内容,即使我使用特殊函数将其从 InputStream 转换为 String(使用 BufferedReader)。

我要存储的字符串称为“RESULT”。

public void connect(String url){
         HttpClient client = new DefaultHttpClient();
         HttpGet httpGet = new HttpGet(url);
         HttpResponse response;

         try{
             response = client.execute(httpGet);

             Log.i("Praeda", response.getStatusLine().toString());

             HttpEntity entity = response.getEntity();

             if(entity != null){
                 result = entity.getContent().toString();
             }

             if(entity == null){
                 result = "failed";
             }
             }catch(Exception e){
             e.printStackTrace();
         }
    }

您可能有任何建议都会很棒...

【问题讨论】:

  • 您是否验证了服务器确实在发送您认为它正在发送的 JSON?
  • 它生成的页面只是带有一个有效 JSON 字符串的 HTML。 (经过测试,因此它是有效的 JSON)。问题是它没有从页面中获取任何内容:www.ace.ucv.ro/android/android
  • 会不会是数据太长了?

标签: android http return


【解决方案1】:

JSON 响应通常是 gzip 压缩的,试试这个

jsonResponse = client.execute(httpGet);
InputStream in = response.getEntity().getContent();
GZIPInputStream gin = new GZIPInputStream(in);
BufferedReader reader = new BufferedReader(new InputStreamReader(gin));
String line;
while ((line = reader.readLine()) != null) {
    jsonResponse.append(line);
}
reader.close();

【讨论】:

  • 我会尽快试一试的!非常感谢您的回答!
  • 使用了 StringBuilder!无论如何,非常感谢您的帮助!这对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多