java 类型不匹配:无法从 org.apache.http.HttpResponse 转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13183111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:47:25  来源:igfitidea点击:

Type mismatch: cannot convert from org.apache.http.HttpResponse

javaandroidhttprequest

提问by user1781367

I'm trying to update a MySQL data in Android by making HTTP Request but it gives me an error

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

"Type mismatch: cannot convert from org.apache.http.HttpResponse to com.google.api.client.http.HttpResponse"

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

on this line of code "httpClient.execute(httpPost)"

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

HttpResponse response = httpClient.execute(httpPost);

and it gives and option to quick fix by adding org.apache.http like

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

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

Could someone tell me what I'm doing wrong here? Thank you so much!

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

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

回答by kosa

You have wrong import statement in your code.

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

goto top of your class and look for

转到班级的顶部并寻找

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

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

replace it with

将其替换为

import org.apache.http.HttpResponse

回答by Imran Ali

Use import org.apache.http.HttpResponse;instead of import com.google.api.client.http.HttpResponse;

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

回答by urveshpatel50

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

回答by Yogendra Singh

You would be having

你会有

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

in your imports list at the top of the source code.

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

Please update that to

请将其更新为

    import org.apache.http.HttpResponse;.

httpClient.execute(httpPost);is returning org.apache.http.HttpResponseobject and by having incorrect import type, you are trying to cast it to com.google.api.client.http.HttpResponse, which is cause the issue.

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

回答by ecbrodie

org.apache.http.HttpResponseand com.google.api.client.http.HttpResponseare two completely separate and different Java classes. You cannot just simply take one and convert it to the other. If you are using Apache's HttpClient, then it will always return org.apache.http.HttpResponse, not the Android version. If you indeed want to use Apache's HttpClient, then I suggest you just stick to the Apache version instead of the Android version of HttpResponse and extract headers/content directly as needed.

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