user1781367
user1781367

Reputation: 602

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

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

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

on this line of code "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.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;
    }

Upvotes: 0

Views: 16309

Answers (5)

urveshpatel50
urveshpatel50

Reputation: 1685

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

Upvotes: 2

Imran Ali
Imran Ali

Reputation: 539

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

Upvotes: 2

ecbrodie
ecbrodie

Reputation: 11916

org.apache.http.HttpResponse and com.google.api.client.http.HttpResponse are 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.

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34367

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.HttpResponse object and by having incorrect import type, you are trying to cast it to com.google.api.client.http.HttpResponse, which is cause the issue.

Upvotes: 1

kosa
kosa

Reputation: 66657

You have wrong import statement in your code.

goto top of your class and look for

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

replace it with

import org.apache.http.HttpResponse

Upvotes: 6

Related Questions