如何使用Get方法在android中调用Web服务

在我的程序中,我使用postmethod来调用Web服务。但是Web服务基于Get方法。我不知道如何编写Get方法的代码。请告诉我如何编写Get方法来调用Web服务的代码。我的代码如下所示基于post方法。 RestClient arc = new RestClient(“http:..............”); arc.AddParam(“search”,msft); 在这里,我创建了一个RestClient对象,并通过调用AddParam方法添加参数.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();
}
} 所以请告诉我如何编写get方法的代码来代替post方法或告诉我解决方案 先感谢您     
已邀请:
这会有用吗?我最近没有对此进行测试,但它对于我曾经有过一段时间的学校项目非常有效。它将返回一个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;
        }
    

要回复问题请先登录注册