2010-12-24 8 views
0

私のプログラムでは、Webサービスを呼び出すためにpostmethodを使用しています。しかし、WebサービスはGetメソッドに基づいています。Getメソッドのコードを記述する方法はわかりません。Webサービスを呼び出すGetメソッドのコードを記述する方法を教えてください。私のコードは、postメソッドに基づいて以下に示されています。使用方法androidのWebサービスを呼び出すための取得メソッド

RestClient arc = new RestClient( "http:.............."); arc.AddParam( "search"、msft);ここで

iはADDPARAM method.The RestClientクラスを呼び出すことによってRestClientと追加のパラメータにオブジェクトを作成している

パブリッククラスAddSubRestClient {

private ArrayList <NameValuePair> params; 
private ArrayList <NameValuePair> headers; 
private InputStream instream; 
private String url; 

private int responseCode; 
private String message; 

private String response; 

public String getResponse() { 
    return response; 
} 
public InputStream inResponse() { 
    return instream; 
} 
public String getErrorMessage() { 
    return message; 
} 

public int getResponseCode() { 
    return responseCode; 
} 

public AddSubRestClient(String url) 
{ 
    this.url = url; 
    params = new ArrayList<NameValuePair>(); 
    headers = new ArrayList<NameValuePair>(); 
} 

public void AddParam(String name, String value) 
{ 
    params.add(new BasicNameValuePair(name, value)); 
} 

public void AddHeader(String name, String value) 
{ 
    headers.add(new BasicNameValuePair(name, value)); 
} 

public void Execute() throws Exception 
{ 
    //Postmethod 
    HttpPost request = new HttpPost(url); 
    Log.e("in","rest client"); 
    //add headers 
    for(NameValuePair h : headers) 
    { 
     request.addHeader(h.getName(), h.getValue()); 
    } 

    if(!params.isEmpty()){ 
     UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
     request.setEntity(p_entity); 
     Log.e("params",params.toString()); 
     Log.e("request",request.toString()); 
    } 
     executeRequest(request, url); 

    } 


public void executeRequest(HttpUriRequest request, String url) 
{ 
    HttpClient client = new DefaultHttpClient(); 
    HttpResponse httpResponse; 

    try { 
     httpResponse = client.execute(request); 
     responseCode = httpResponse.getStatusLine().getStatusCode(); 
     message = httpResponse.getStatusLine().getReasonPhrase(); 
     HttpEntity entity = httpResponse.getEntity(); 
     if (entity != null) { 
     intream = entity.getContent(); 
     response = convertStreamToString(instream); 
    /Closing the input stream will trigger connection release 
      instream.close(); 
     } 

    } catch (ClientProtocolException e) { 
     client.getConnectionManager().shutdown(); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     client.getConnectionManager().shutdown(); 
     e.printStackTrace(); 
    } 
} 

private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     Log.e("in","try block"); 
     line = reader.readLine(); 
     Log.e("line",line); 
     if(line==null) 
     { 
      Log.e("Line","is null"); 
     } 
     else{ 
      Log.e("Line","is not null"); 
      sb.append(line); 

     } 
     } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      Log.e("line","close"); 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

}

ので、どのように書く方法を教えてくださいポストメソッドの代わりにメソッドを取得するためのコードまたはソリューションを教えてください

ありがとう

答えて

0

これは機能しますか?私は最近これをテストしていませんが、私はしばらく前にしていた学校プロジェクトには完全に機能しました。 JSONObjectが返されますが、必要に応じて変更することはできます。また、パラメータはGETを呼び出すURLの文字列です。

public static JSONObject connectSingle(String url) 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 

      // Prepare a request object 
      HttpGet httpget = new HttpGet(url); 

      // Execute the request 
      HttpResponse response; 
      try { 
       response = httpclient.execute(httpget); 

       // Get hold of the response entity 
       HttpEntity entity = response.getEntity(); 
       // If the response does not enclose an entity, there is no need 
       // to worry about connection release 

       if (entity != null) 
       { 

        // A Simple JSON Response Read 
        InputStream instream = entity.getContent(); 
        String result= convertStreamToString(instream); 
        instream.close(); 

        // A Simple JSONObject Creation 
        JSONObject obj = new JSONObject(result); 
        return obj; 
       } 
       else 
        return null; 

      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 
+0

私の関連する質問をご覧ください:http://stackoverflow.com/questions/30092261/using-volley-library-inside-a-library –

関連する問題