Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android从URL检索JSON对象_Java_Android_Json - Fatal编程技术网

Java Android从URL检索JSON对象

Java Android从URL检索JSON对象,java,android,json,Java,Android,Json,我正在开发一个应用程序,该应用程序对响应JSON对象的php脚本进行API调用。通过浏览器手动测试php文件会返回预期的信息,但我的应用程序的行为就像返回的字符串是空的一样(甚至在我开始解码JSON对象之前) 下面是我的代码片段。我已经在我的应用程序中多次成功地将此脚本用于回显字符串的api String urlParameters = "request=item_search&item_num=" + barcode + "&ou=" + OU + "&use

我正在开发一个应用程序,该应用程序对响应JSON对象的php脚本进行API调用。通过浏览器手动测试php文件会返回预期的信息,但我的应用程序的行为就像返回的字符串是空的一样(甚至在我开始解码JSON对象之前)

下面是我的代码片段。我已经在我的应用程序中多次成功地将此脚本用于回显字符串的api

String urlParameters = 
    "request=item_search&item_num=" + barcode + "&ou=" + OU + "&user_tag=" + initials + "&version=" + version + "&scan_point=return";
URL url = null;
try {
        if (testMode == true) 
        {
            url = new URL("http://URL/api.php");
        } 
        else 
        {
            url = new URL("http://URL/api.php");
        }
    } 
    catch (MalformedURLException e) 
    {
        e.printStackTrace();
    }
    StringBuilder output = new StringBuilder();
    try 
    {
        assert url != null;
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(urlParameters);
        writer.flush();
        writer.close();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        while ((line = reader.readLine()) != null)
        {
            output.append(line);
        }
        writer.close();
        reader.close();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    String outputString = output.toString();
你试过了吗

HTTP是现代网络应用的方式。这就是我们交换数据和媒体的方式。高效地使用HTTP可以加快加载速度并节省带宽

您可以尝试:

有关更多信息,请访问:


如果您不幸看到并得到,则该应用程序使用的是defaultHttpClient类,该类已被弃用。然而,查看代码,我们仍然以相同的方式读取URL。我不知道为什么我的字符串出于某种原因是空的,但是,如果你的服务器运行的是最新版本的apache和php。发送一个折旧的标题将不会给您任何您已经发现的东西。有没有可能让你的应用程序发送一个不同的头,就像我之前所说的那样,URLConnection类可以很好地从API获取字符串形式的响应(我的应用程序中几乎所有其他内容都使用它)。问题是,对于我正在实现的这个特定功能,我必须使用其他人的API(仍在同一台服务器上运行)来回显JSON对象,而不是字符串,但由于某种原因,字符串在有机会被解析之前返回为空。@KGillis我已经发布了我的答案,您可以检查它。
package com.squareup.okhttp.guide;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;

public class GetExample {
  OkHttpClient client = new OkHttpClient();

  String run(String url) throws IOException {
    Request request = new Request.Builder()
        .url(url)
        .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
  }

  public static void main(String[] args) throws IOException {
    GetExample example = new GetExample();
    String response = example.run("https://raw.github.com/square/okhttp/master/README.md");
    System.out.println(response);
  }
}