2011-08-18 73 views
0

对不起,这个简单的(?)问题,但我是一个java和android新手。ANDROID StringEntity内部失败

首先我有这个导入部分。

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.view.View.OnClickListener; 

import java.util.List; 
import java.util.ArrayList; 

import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpEntity; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.NameValuePair; 
import org.apache.http.entity.*; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.entity.StringEntity; 

import org.apache.http.client.entity.UrlEncodedFormEntity; 

然后我有这段代码。

  String s = new String(); 
      // Handle successful scan 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(URL); 

      s += "enter=<eanrequest><ean>"; 
      s += contents; 
      s += "</ean></eanrequest>";    

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); 
      nameValuePairs.add(new BasicNameValuePair("enter", s)); 

      ean.setText(s); 

      HttpEntity se = new StringEntity(s); //When I hold the mouse over this I get "Unhandled exception type UnsupportedEncodingException". 

我在做什么错?

/埃里克

+0

您是说在导入之后,您只需发布代码,而无需任何类! (无论如何,导入部分根本不相关,你可以随时访问Source-Organize Imports,Eclipse会照顾它们) – Adinia

+0

我不知道这个关于eclipse的感谢。 – Erik

+0

因此,如果您在发布的代码旁边还有一个类/活动,则从您收到的消息中,我认为它只是希望您将该行包装在try/catch块中。 – Adinia

回答

1

尝试用:

try{ 
    HttpEntity se = new StringEntity(s); 
}catch(Exception e){ 

} 

的StringEntity构造函数可以抛出,你必须处理异常。您可以将其封装在try/catch块中,也可以声明您的方法可能会抛出异常:

public void myMethod() throws Exception{ 
    //... 
    HttpEntity se = new StringEntity(s); 
    //... 
}