HttpPost返回一个json字符串不同的浏览器之一

时间:2013-03-12 15:58:39

标签: android json post https

当我去

  

https://api.github.com/users/jkirkell/gists

我收到了一个包含良好数据的json。

[
  {
    "url": "https://api.github.com/gists/5143977",
    "forks_url": "https://api.github.com/gists/5143977/forks",
    "commits_url": "https://api.github.com/gists/5143977/commits",
    "id": "5143977",
    etc.

但如果我用这段代码读取SAME地址:

String jsonString = null;
InputStream is = null;

HttpResponse response = null;
try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("https://api.github.com/users/jkirkell/gists");
        response = httpclient.execute(httppost);
}catch(Exception e){
        throw e;
}

我收到了这个json字符串:

{"message":"Not Found"}

我的代码有什么问题?

2 个答案:

答案 0 :(得分:3)

Get请求,但您正在使用HTTP帖子。

尝试阅读

int k = 0;

   URL url = new URL(yoururl);
         InputStream input=url1.openStream();
         BufferedInputStream bis=new BufferedInputStream(input);
         ByteArrayBuffer baf=new ByteArrayBuffer(1000);
         while((k=bis.read())!=-1)
         {
         baf.append((byte)k);
         }
        String data=new String(baf.toByteArray());

答案 1 :(得分:2)

您应该使用HttpGet代替HttpPost

HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet("https://api.github.com/users/jkirkell/gists");
response = httpclient.execute(request);