0

我正在尝试通过发出 HTTP 请求来更新 Android 中的 MySQL 数据,但它给了我一个错误

“类型不匹配:无法从 org.apache.http.HttpResponse 转换为 com.google.api.client.http.HttpResponse”

在这行代码“httpClient.execute(httpPost)”

HttpResponse response = httpClient.execute(httpPost);

它通过添加 org.apache.http 来提供快速修复的选项

org.apache.http.HttpResponse response = httpClient.execute(httpPost);

有人可以告诉我我在这里做错了什么吗?太感谢了!

protected String doInBackground(String... params) {

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://---.com/---/approve_request.php");

    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    nameValuePair.add(new BasicNameValuePair("status", "Approved"));

    try {
          httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {              
           e.printStackTrace();
        }

           try {
              org.apache.http.HttpResponse response = httpClient.execute(httpPost);  
              Log.d("Http Response:", response.toString());

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

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

         }

    return null;
    }
4

5 回答 5

5

您的代码中有错误的导入语句。

转到你的班级顶部并寻找

import com.google.api.client.http.HttpResponse

将其替换为

import org.apache.http.HttpResponse
于 2012-11-01T18:17:24.250 回答
2
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;

     InputStream is = null;
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("status", "Approved"));


        try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                ""http://---.com/---/approve_request.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());
    }
于 2012-11-01T18:32:31.823 回答
2

使用import org.apache.http.HttpResponse; 代替import com.google.api.client.http.HttpResponse;

于 2012-11-01T18:21:59.803 回答
1

你会有

    import com.google.api.client.http.HttpResponse;

在源代码顶部的导入列表中。

请更新为

    import org.apache.http.HttpResponse;.

httpClient.execute(httpPost);正在返回org.apache.http.HttpResponse对象,并且由于导入类型不正确,您正试图将其强制转换为com.google.api.client.http.HttpResponse,这是导致问题的原因。

于 2012-11-01T18:17:57.163 回答
1

org.apache.http.HttpResponse并且com.google.api.client.http.HttpResponse是两个完全独立且不同的 Java 类。您不能只是简单地拿一个并将其转换为另一个。如果您使用的是 Apache 的HttpClient,那么它将始终返回org.apache.http.HttpResponse,而不是 Android 版本。如果您确实想使用 Apache 的 HttpClient,那么我建议您坚持使用 Apache 版本而不是 Android 版本的 HttpResponse,并根据需要直接提取 headers/content。

于 2012-11-01T18:18:24.870 回答