HTTPGET删除参数 [英] Httpget removes parameters

查看:154
本文介绍了HTTPGET删除参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从该网站分析我的序列化的JSON数据:的http:// demos.brianbuikema.com/apps/soa_services/employees?format=JSON
但我的结果是我得到的回复是从该网站的来源$ C ​​$ C:的http://演示.brianbuikema.com /应用/ soa_services / 的员工?
所以,我某处参数格式= JSON将删除。但我不知道如何连接,其中

这是我的code不介意log.d,它只是对我来说,当我调试的调试。 `

 的HttpClient HttpClient的=新DefaultHttpClient();    // prepare请求对象
    HTTPGET HTTPGET =新HTTPGET(http://demos.brianbuikema.com/apps/soa_services/employees?format=JSON);    字符串结果= NULL;
    尝试{
        //执行请求
        Log.d(BUH,2AA);
        HTT presponse响应= httpclient.execute(HTTPGET);
                            Log.d(BUH,2A);
        //获取响应实体持有
        HttpEntity实体= response.getEntity();
        //如果响应未包封的实体,也没有必要
        //担心连接释放
        Log.d(BUH,2b中);
        如果(实体!= NULL){
            //一个简单的响应读取
            Log.d(寮步,2C);
            InputStream的河道= entity.getContent();
            结果= convertStreamToString(插播广告);
            Log.d(寮步,结果);
            //关闭输入流会触发连接释放
            instream.close();
        }
    }赶上(ClientProtocolException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }    返回结果;
}私人静态字符串convertStreamToString(InputStream为){
    / *
     *要InputStream中转换为字符串,我们使用
     * BufferedReader.readLine()方法。我们重复,直到的BufferedReader
     *返回NULL,这意味着没有更多的数据读取。每一行
     *附加​​到一个StringBuilder并返回为String。
     * /
    读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是)
            8192);
    StringBuilder的SB =新的StringBuilder();    串线= NULL;
    尝试{
        而((行= reader.readLine())!= NULL){
            sb.append(行+\\ n);
        }
    }赶上(IOException异常五){
        e.printStackTrace();
    } {最后
        尝试{
            is.close();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }    返回sb.toString();
}`


解决方案

这个错误是从服务器,请求此网址时,它消除了头JSON,
所以你需要添加:
         httpget.addHeader(新BasicHeader(接受,应用/ JSON)); 下的HTTPGET

下面是完整的code:

 的HttpClient HttpClient的=新DefaultHttpClient();    // prepare请求对象
    HTTPGET HTTPGET =新HTTPGET(URL);
    httpget.addHeader(新BasicHeader(接受,应用/ JSON));
    //httpget.getParams().setParameter(\"format,JSON);    Log.d(Z,httpget.getURI()的toString());    //执行请求
    HTT presponse响应;    字符串结果= NULL;
    尝试{
        响应= httpclient.execute(HTTPGET);        //获取响应实体持有
        HttpEntity实体= response.getEntity();
        //如果响应未包封的实体,也没有必要
        //担心连接释放        如果(实体!= NULL){
            //一个简单的响应读取
            InputStream的河道= entity.getContent();
            结果= convertStreamToString(插播广告);
            Log.d(Z,结果);
            //关闭输入流会触发连接释放
            instream.close();
        }
    }赶上(ClientProtocolException E){
        // TODO自动生成catch块
        e.printStackTrace();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }    返回结果;
}私人静态字符串convertStreamToString(InputStream为){
    / *
    *要InputStream中转换为字符串,我们使用使用BufferedReader.readLine()
    *方法。我们重复,直到BufferedReader中返回NULL,这意味着
    *没有更多的数据读取。每一行追加到一个StringBuilder
    *和返回为String。
    * /
    读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是),8192);
    StringBuilder的SB =新的StringBuilder();    串线= NULL;
    尝试{
        而((行= reader.readLine())!= NULL){
            sb.append(行+\\ n);
        }
    }赶上(IOException异常五){
        e.printStackTrace();
    } {最后
        尝试{
            is.close();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }    返回sb.toString();
}

i want to parse my serialized JSON data from the website : http://demos.brianbuikema.com/apps/soa_services/employees?format=JSON but my result that i get back is the source code from the site: http://demos.brianbuikema.com/apps/soa_services/employees? So somewhere my parameters format=JSON will delete. but I have no idea how en where

this is my code don't mind the log.d, it's just for me when i'm debugging debugging. `

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();
}  `

解决方案

The mistake was from the server, when requesting this URL, it removes the header JSON, so you need to add : httpget.addHeader(new BasicHeader("Accept", "application/json")); under the HttpGet

here is the full code:

 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();
}

这篇关于HTTPGET删除参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆