尝试发送给Json时,应用程序在httGet上崩溃了吗?

时间:2013-09-19 10:33:26

标签: java android json

我的应用程序在“((HttpResponse) httpGet).setEntity(new StringEntity(jo.toString(),"UTF-8"));”崩溃并引发异常“java.lang.ClassCastException:org.apache.http.client.methods.HttpGet”。

JSONObject jo = new JSONObject();
    try {
        jo.put("devicetoken", devicetoken);
        URI uri = new URI("http", "praylistws-dev.elasticbeanstalk.com",
                "/rest/list/myprayerlist/"+Helper.email, null, null);

        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(uri);

        // Prepare JSON to send by setting the entity
        ((HttpResponse) httpGet).setEntity(new StringEntity(jo.toString(),
                "UTF-8"));

        // Set up the header types needed to properly transfer JSON
        httpGet.setHeader("Content-Type", "application/json");
        httpGet.setHeader("Accept-Encoding", "application/json");
        httpGet.setHeader("Accept-Language", "en-US");

        // Execute POST
        response = httpClient.execute(httpGet);
        String string_response = EntityUtils.toString(response.getEntity());
        string_resp = string_response += "";
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    save(string_resp);
    return result;

2 个答案:

答案 0 :(得分:0)

如果要将一些数据放入请求正文,则必须使用HttpPost而不是HttpGet。 HttpPost具有以下功能:setEntity(HttpEntity entity)

示例:

JSONObject jo = new JSONObject();
try {
   jo.put("devicetoken", devicetoken);
   URI uri = new URI("http", "praylistws-dev.elasticbeanstalk.com",
             "/rest/list/myprayerlist/"+Helper.email, null, null);

   HttpClient httpClient = new DefaultHttpClient();
   HttpPost httpPost = new HttpPost(uri);

   // Prepare JSON to send by setting the entity
   httpPost.setEntity(new StringEntity(jo.toString(), "UTF-8"));

   // Set up the header types needed to properly transfer JSON
   httpGet.setHeader("Content-Type", "application/json");
   httpGet.setHeader("Accept-Encoding", "application/json");
   httpGet.setHeader("Accept-Language", "en-US");

   // Execute POST
   HttpResponse response = httpClient.execute(httpPost);
   String string_response = EntityUtils.toString(response.getEntity());
   string_resp = string_response += "";
} catch (Exception ex) {
   ex.printStackTrace();
}
save(string_resp);
return result;

答案 1 :(得分:0)

活动{ 在OnCreate { new HitService()。execute(addparams here); } }

protected String doInBackground(String ... params){

                String result = null;

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet("http://your url=" + params[0]);


                HttpResponse response;
                try { 
                        response = httpClient.execute(httpGet);                
                        result = EntityUtils.toString(response.getEntity());         

                } catch (ClientProtocolException e) {

                        e.printStackTrace();
                } catch (IOException e) {

                        e.printStackTrace();
                }

                return result;
        }