ANDROID StringEntity在eclipse中失败

时间:2011-08-18 08:37:52

标签: android

对这个简单的(?)问题感到抱歉,但我是java和android的新手。

首先我有这个导入部分。

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".

我做错了什么?

/埃里克

1 个答案:

答案 0 :(得分:1)

尝试:

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

}

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

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