ANDROID StringEntity fail inside eclipse
Sorry about this simple(?) question but I'm a newbie on java and andr开发者_开发问答oid.
First I have this import section.
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;
Then I have this code.
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".
What am I doing wrong?
/Erik
Try with:
try{
HttpEntity se = new StringEntity(s);
}catch(Exception e){
}
The StringEntity constructor can throw an exception that you have to handle. You can either wrap it in a try/catch block or declare that your method can throw an exception:
public void myMethod() throws Exception{
//...
HttpEntity se = new StringEntity(s);
//...
}
精彩评论