2012-11-01 35 views
0

我试图通过使HTTP请求,以更新的Android MySQL的数据,但它给了我一个错误类型不匹配:不能转换从org.apache.http.HttpResponse

“类型不匹配:不能从组织转变。 apache.http.HttpResponse到com.google.api.client.http.HttpResponse”

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

HttpResponse response = httpClient.execute(httpPost); 

,它给和选项来快速修复通过添加org.apache.http like

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

您在代码中导入语句错误。你的类

返回顶部,并查找

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

import org.apache.http.HttpResponse 
+0

烨。得到它了。谢谢 – user1781367

+0

@nana:很高兴帮助。祝你好运。 – kosa

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,这是造成问题。

+0

谢谢你的帮助 – user1781367

1

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

2

使用import org.apache.http.HttpResponse; 而不是import com.google.api.client.http.HttpResponse;

1
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()); 
    } 
相关问题