2011-08-18 70 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); 
    //... 
}