0

Sorry about this simple(?) question but I'm a newbie on java and android.

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

3
  • Do you mean after the imports you just have the code you posted, without any class!? (anyway, the imports part is not relevant at all for a queston, you can always just go to Source-Organize Imports, and Eclipse will take care of them for you) Commented Aug 18, 2011 at 8:59
  • I didn't know this about eclipse thanks. Commented Aug 18, 2011 at 9:05
  • So, if you also have a class/activity beside the code you posted, from the message you get, I think it just want you to wrap that line in a try/catch block. Commented Aug 18, 2011 at 9:12

1 Answer 1

1

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);
    //...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.