首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android rest rest服务问题

Android rest rest服务问题
EN

Stack Overflow用户
提问于 2011-03-16 13:28:09
回答 1查看 449关注 0票数 0
代码语言:javascript
复制
package com.rest;
import java.io.IOException;  

import android.app.Activity;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.Toast;

import org.apache.http.client.ClientProtocolException;  
import org.apache.http.client.HttpClient;  
import org.apache.http.client.ResponseHandler;  
import org.apache.http.impl.client.BasicResponseHandler;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.impl.client.DefaultHttpClient;  
import android.app.Activity;
import android.os.Bundle;

public class rest extends Activity {
    /** Called when the activity is first created. */

    String URL = "http://sc2.mystreamserver.com:8050/admin.cgi?mode=viewxml";  
       String result = "";  
        String deviceId = "xxxxx" ;  
       final String tag = "Your Logcat tag: ";  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);




          /** Called when the activity is first created. */  

        callWebService();  

                final EditText txtSearch = (EditText)findViewById(R.id.txtSearch);  
             txtSearch.setOnClickListener(new EditText.OnClickListener(){  
                public void onClick(View v){txtSearch.setText("");}  
             });  

               final Button btnSearch = (Button)findViewById(R.id.btnSearch);  
                btnSearch.setOnClickListener(new Button.OnClickListener(){  
                    public void onClick(View v) {  
                        String query = txtSearch.getText().toString();  


                   }  
               });  

          } // end onCreate()  

           public void callWebService(){  
              HttpClient httpclient = new DefaultHttpClient();  
               HttpGet request = new HttpGet(URL);  
               request.addHeader("username","    "); 
               request.addHeader("pass","   ");  
               ResponseHandler<String> handler = new BasicResponseHandler();  
               try {  
                  result = httpclient.execute(request, handler);  
                } catch (ClientProtocolException e) {  
                    result=e.toString(); 
                } catch (IOException e) {  
                    result=e.toString();  
                }  
                httpclient.getConnectionManager().shutdown();  
               Log.i(tag, result);  
               Toast.makeText(rest.this, result, Toast.LENGTH_SHORT).show();
            } // end callWebService()  

 }

我没有获得result.plz帮助来获得结果...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-16 13:48:24

请试一下这个。

代码语言:javascript
复制
public void callWebService()
{  
    HttpPost postMethod = new HttpPost("Your Url");
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("username","your user name");
    nameValuePairs.add(new BasicNameValuePair("password","your password");
    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    DefaultHttpClient hc = new DefaultHttpClient();

    HttpResponse response = hc.execute(postMethod);
    HttpEntity entity = response.getEntity();

    if (entity != null) 
    {
            InputStream inStream = entity.getContent();
            result= Utility.convertStreamToString(inStream);
            Log.i("---------------- Result",result);
    }
} // end callWebService()  

代码语言:javascript
复制
public static String convertStreamToString(InputStream is)
{
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuilder sb = new StringBuilder();

   String line = null;
   try 
   {
       while ((line = reader.readLine()) != null) 
       {
           sb.append(line + "\n");
       }
   } 
   catch (IOException e) 
   {
       e.printStackTrace();
   } 
   finally 
   {
       try 
       {
           is.close();
       } 
       catch (IOException e) 
       {
           e.printStackTrace();
       }
   }
   return sb.toString();

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5321417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档