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帮助获得结果....
答案 0 :(得分: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();
}