2011-03-16 41 views
0
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帮助来获得结果....Android的休息WebService的问题

+0

差的问题是很差。努力编辑你的问题并解释它。 – Reno 2011-03-16 05:41:58

+0

@reno ok谢谢你... – 2011-03-16 05:46:27

回答

1

请试试这个。

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() 

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(); 

}

+0

thanx chirag它的工作 – 2011-03-16 08:56:14

+0

okiii但我不能投票bcoz我有较少的声誉 – 2011-03-16 09:42:45

+0

如果我想发送一个大文件(也许1mb),而不是字符串内容,那么会怎么做? convertStreamToString需要不同还是分开?什么将取代Stringbuilder? – twerdster 2011-04-12 23:27:41