2011-07-19 4 views
1

私のアプリケーションでは、サーバーからいくつかの割り当てのリストを取得する必要があります。サーバー上では、私はfetchAssignments(int、String)という2つの値をパラメータとするメソッドを持っています。httpサーバからxmlストリームを取得中 - android problem

  1. 期間(今日、現在の月または現在の週) - 整数
  2. ユーザID - 文字列

この関数は、XMLストリームとして割り当てのリストを返します。私はhttpサーバにどのように接続できるか知っています。しかし、私はどのように私はサーバー上でそのメソッドを呼び出すことができないし、これらのパラメータを渡す。誰も私にそれをするより良い方法を提案することができます...?

答えて

1

言語に応じて、URL HTTPリクエストまたはPOSTを使用してパラメータを渡すことができます。クラスのコンストラクタ(そのWebページ?php?.aspx?と仮定して)これらの値を取得し、上記のメソッドに渡しますか?

try{   
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.yoursite.com/getfeed.php"); 
InputSource inStream = new InputSource(); 

try { 
// Add data 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("period", period)); 
nameValuePairs.add(new BasicNameValuePair("userid", user_id)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity r_entity = response.getEntity(); 
String xmlString = EntityUtils.toString(r_entity); 

xmlString = xmlString.trim(); 
InputStream in = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); 

if(xmlString.length() != 0){ 
inStream.setByteStream(in); 
PARSE_FLAG = true; 
} 
else 
{ 
PARSE_FLAG = false; 
} 

} catch (ClientProtocolException e) { 
// TODO Auto-generated catch block 
} catch (IOException e) { 
// TODO Auto-generated catch block 
} 
3

あなたは、HTTP GETリクエストを使用して、サーバーからのInputStreamとしてXMLを要求し、リクエストパラメータとしてパラメータを渡すだけでできますからストリームを取得することができます以下のような方法が何かを

http://some.server/webapp?period=1&userid=user1 

をサーバー:

/** 
* Returns an InputStream to read from the given HTTP url. 
* @param url 
* @return InputStream 
* @throws IOException 
*/ 
public InputStream get(final String url) throws IOException { 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpParams httpParams = httpClient.getParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT); 
    HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT); 
    HttpGet httpget = new HttpGet(url); 
    HttpResponse httpResponse = httpClient.execute(httpget); 
    StatusLine statusLine = httpResponse.getStatusLine(); 
    if(! statusLine.getReasonPhrase().equals("OK")) { 
     throw new IOException(String.format("Request failed with %s", statusLine)); 
    } 
    HttpEntity entity = httpResponse.getEntity(); 
    return entity.getContent(); 
} 

そしてあなたはJAXBのようなエンティティにXMLを解析するために、 "シンプル"(http://simple.sourceforge.net/)XMLライブラリを使用することができます。

/** 
* Reads the XML from the given InputStream using "Simple" and returns a list of assignments. 
* @param InputStream 
* @return List<Assignment> 
*/ 
public List<Assignment> readSimple(final InputStream inputStream) throws Exception { 

    Serializer serializer = new Persister(); 

    return serializer.read(AssignmentList.class, inputStream).getAssignments();  
} 

私は、RESTサービスを使っているだけなので、リクエストパラメータは使用しません。

+0

うんは、私にはいいですね:) 1 – tutts

+0

HTTPのPOSTが優れているサポートされている場合、バック+1 :-) –

+0

パラメータが一致した場合、それはサーバー上で、それぞれの関数を呼び出すん......?私が言ったように、私はサーバー上でfetchAssignment(int、string)を呼び出す必要があります... – Kishan

関連する問題