从Android读取JSON内容

从Android读取JSON内容,android,json,curl,Android,Json,Curl,我在从URL读取JSON时遇到问题 对于桌面应用程序,我一直使用“curl”从URL读取内容 i、 e.curl-H“接受:应用程序/json”http://example.com/abc.py/data“ 我正在使用以下代码在Android应用程序中执行相同的操作,但它不起作用 HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url);

我在从URL读取JSON时遇到问题

对于桌面应用程序,我一直使用“curl”从URL读取内容

i、 e.
curl-H“接受:应用程序/json”http://example.com/abc.py/data“

我正在使用以下代码在Android应用程序中执行相同的操作,但它不起作用

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.addHeader("Accept", "application/json"); 
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
有人能帮忙吗


谢谢,

它可能不起作用有很多原因,其中一个原因是它实际上是getContent返回的inputStream,下面是我的应用程序中的一段工作代码

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


...


private static String getInputStreamAsString(Context ctxt, InputStream is)
{
    byte[] sBuffer = new byte[512];
    ByteArrayOutputStream content = new ByteArrayOutputStream();

    // Read response into a buffered stream
    int readBytes = 0;
    try
    {
        while ((readBytes = is.read(sBuffer)) != -1)
        {
            content.write(sBuffer, 0, readBytes);
        }
    }
    catch (IOException e)
    {
        Util.e(ctxt, TAG, Util.fmt(e));
    }
    return new String(content.toByteArray());
}


public static test() {
    HttpClient client = DefaultHttpClient()
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);;
    StatusLine status = response.getStatusLine();
    String str = getInputStreamAsString(ctxt, response.getEntity().getContent());
    JSONObject json = new JSONObject(str);
}

...

谢谢大家的回复

发生错误的原因是我没有在清单中设置Internet权限,因此无法连接到URL


它现在可以工作了:)

您是否正在尝试解析android应用程序中的JSON?