JSON file not getting downloaded, function returns null

后端 未结 3 1842
刺人心
刺人心 2021-01-24 08:34

I am trying to download some JSON from the google book API.

The URL and API-Key I\'m using seems to work becuase i can fetch it manually with a browser. I call this cl

3条回答
  •  我在风中等你
    2021-01-24 09:31

    I use this code to get response from webservice and it works fine

    try {
                    HttpPost request = new HttpPost(
                            "webservice url");
                    request.setHeader("Accept", "application/json");
                    request.setHeader("Content-type", "application/json");
                    // Build JSON string
                    JSONStringer loginuser = new JSONStringer().object().key("userid")
                            .value(SetGetValues.getUserid()).endObject();
                    StringEntity entity = new StringEntity(loginuser.toString());
                    request.setEntity(entity);
                    Log.v("data", loginuser.toString());
                    // Send request to WCF service
                    DefaultHttpClient httpClient1 = new DefaultHttpClient();
                    HttpResponse response = httpClient1.execute(request);
                    Log.v("response code", response.getStatusLine().getStatusCode()
                            + "");
                    HttpEntity responseEntity = response.getEntity();
                    // Read response data into buffer
                    char[] buffer = new char[(int) responseEntity.getContentLength()];
                    InputStream stream = responseEntity.getContent();
                    InputStreamReader reader = new InputStreamReader(stream);
                    reader.read(buffer);
                    stream.close();
                    results = new JSONArray(new String(buffer));
        }catch(Exception e){
        // TODO: handle exception
                    e.printStackTrace();
    }
    

    check out the following line

    Log.v("response code", response.getStatusLine().getStatusCode()
                                + "");
    

    if its value is 200 then you r getting ok response from server else you are not getting response from server

提交回复
热议问题