0

我的应用程序在“”上崩溃((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;
4

2 回答 2

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;
于 2013-09-19T10:47:37.870 回答
0

Activity{ oncreate{ new HitService().execute(addparams here); } }

受保护的字符串doInBackground(字符串...参数){

                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;
        }
于 2013-09-19T11:06:09.150 回答