2011-09-13 4 views
2

私のコンピュータがインターネットに接続しようとすると、私はプロキシ、ユーザ名とパスワードを設定しなければなりません。私はAndroid仮想マシンに設定して動作させます(仮想マシンはインターネットにアクセスできます)。しかし、私は私のアプリを実行すると、インターネットにアクセスすることはできません。 誰でも助けてくれますか?ありがとうございます!Android:About android webservice

public class wsActivity extends Activity { 
    private static final String mNameSpace = "http://WebXml.com.cn/"; 
    private static final String mMethodName = "getWeatherbyCityName"; 
    private static final String mUrl = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx?wsdl"; 
    private static final String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; 
    private String weatherToday = null; 
    private SoapObject details; 

private Button mBtnSearch; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mBtnSearch = (Button)findViewById(R.id.btn_search); 
    mBtnSearch.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      getWeather("北京"); 
     } 
    }); 

} 

/** 
* getWeather(String cityName) 
* 
*/ 
public void getWeather(String cityName){ 
    SoapObject rpc = new SoapObject(mNameSpace, mMethodName); 
    rpc.addProperty("theCityName", cityName); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.bodyOut = rpc; 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(rpc); 


    HttpTransportSE ht = new HttpTransportSE(mUrl); 
    //AndroidHttpTransport ht = new AndroidHttpTransport(mUrl); 
    ht.debug = true; 
    Log.d("getWeather","path:"+ht.getPath()); 
    try { 
     ht.call(SOAP_ACTION, envelope); 
     details = (SoapObject) envelope.getResponse(); 
     Log.d("getWeather",details.toString()); 
     parseWeather(details); 
     return; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } 
} 

/** 
* parseWeather(SoapObject details) 
* 
*/ 
public void parseWeather(SoapObject detail) throws UnsupportedEncodingException { 
    String date = detail.getProperty(6).toString(); 
    weatherToday = "今天:" + date.split(" ")[0]; 
    weatherToday = weatherToday + "\n天气:" + date.split(" ")[1]; 
    weatherToday = weatherToday + "\n气温:" 
      + detail.getProperty(5).toString(); 
    weatherToday = weatherToday + "\n风力:" 
      + detail.getProperty(7).toString() + "\n"; 

    Toast.makeText(this, weatherToday, Toast.LENGTH_LONG).show(); 
} 

}

+0

マニフェストのインターネットのアクセス許可を設定しましたか – Armand

答えて

3

あなたのコードは、あなたがKSoap2を使用している示しています。次のように、クラスServiceConnectionSEのコンストラクタを変更することができます

public ServiceConnectionSE(String url) throws IOException { 
    connection = (HttpURLConnection) new URL(url).openConnection(); 
    connection.setUseCaches(false); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 
} 

:KSoap2では次のコンストラクタを持つクラスServiceConnectionSEがあります。あなたものコピーを作成する必要がありますことを、に注意してください

public ServiceProxyConnectionSE(
     String url, String user, String passwd) throws IOException {   

    String s = user + ":" + passwd; 
    new Base64(); 
    String strBase64 = "Basic " + Base64.encode(s.getBytes()); 

    connection = (HttpURLConnection) new URL(url).openConnection(); 
    connection.setUseCaches(false); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 
    connection.setRequestProperty("Authorization", strBase64); 

} 

:より良い方法は、しかしServiceConnectionSEクラスのコピーを作成(および例えばServiceProxyConnectionSEという名前を付けます)、次のようにコンストラクタを実装することですHTTPTransportSEクラス(例:HTTPProxyTransportSE)を開き、メソッドgetServiceConnectionを新しいServiceProxyConnectionクラスに変更してください!

+0

[ksoap2-androidとWFS]に関する私の質問にお手伝いしてください。(http://stackoverflow.com/questions/9237082/how-to-query -a-web-service-by-post-request-in-android)? – JJD