Httpget删除参数

时间:2012-03-15 13:15:48

标签: android url parameters http-get

我想从网站解析我的序列化JSON数据:http://demos.brianbuikema.com/apps/soa_services/employees?format=JSON 但是我回来的结果是来自网站的源代码:http://demos.brianbuikema.com/apps/soa_services/employees? 所以我的参数format = JSON会删除。但我不知道怎么在哪里

这是我的代码,不介意log.d,当我调试调试时,它只适合我。 `

HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object


    HttpGet httpget = new HttpGet("http://demos.brianbuikema.com/apps/soa_services/employees?format=JSON");

    String result = null;
    try {
        // execute the request
        Log.d("buh", "2aa");
        HttpResponse response = httpclient.execute(httpget);
                            Log.d("buh", "2a");
        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release
        Log.d("buh", "2b");
        if (entity != null) {
            // A Simple Response Read
            Log.d("buh", "2c");
            InputStream instream = entity.getContent();
            result = convertStreamToString(instream);
            Log.d("buh",result);
            // Closing the input stream will trigger connection release
            instream.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the
     * BufferedReader.readLine() method. We iterate until the BufferedReader
     * return null which means there's no more data to read. Each line will
     * appended to a StringBuilder and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is),
            8192);
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}  `

2 个答案:

答案 0 :(得分:1)

错误来自服务器,在请求此URL时,它会删除标头JSON, 所以你需要添加:         HttpGet下的httpget.addHeader(new BasicHeader("Accept", "application/json"));

这是完整的代码:

 HttpClient httpclient = new DefaultHttpClient();

    // Prepare a request object
    HttpGet httpget = new HttpGet(url);
    httpget.addHeader(new BasicHeader("Accept", "application/json"));
    //httpget.getParams().setParameter("format", "JSON");

    Log.d("z",httpget.getURI().toString());

    // Execute the request
    HttpResponse response;

    String result = null;
    try {
        response = httpclient.execute(httpget);

        // Get hold of the response entity
        HttpEntity entity = response.getEntity();
        // If the response does not enclose an entity, there is no need
        // to worry about connection release

        if (entity != null) {
            // A Simple Response Read
            InputStream instream = entity.getContent();
            result = convertStreamToString(instream);
            Log.d("z",result);
            // Closing the input stream will trigger connection release
            instream.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

private static String convertStreamToString(InputStream is) {
    /*
    * To convert the InputStream to String we use the BufferedReader.readLine()
    * method. We iterate until the BufferedReader return null which means
    * there's no more data to read. Each line will appended to a StringBuilder
    * and returned as String.
    */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

答案 1 :(得分:0)

您需要设置名称值对。 。关注这个..

protected String doInBackground(String... params) {
        String result = "";
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// REPLACE param [0] or one with your value of variable like _userid or etc..
        nameValuePairs.add(new BasicNameValuePair("userid",params[0]));

        try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://thisismywebsite.com/mypage.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        } catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

        try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();     
                result=sb.toString().trim();                
        } catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }

        return result;
    }

我希望,对你有用。