Android rest Web服务问题 package com.rest; 导入java.io.IOException; 导入android.app.Activity; 导入android.os.Bundle; 导入android.util.Log; 导入android.view.view; 导入android.widget.Button; 导入android.widget.EditText; 导入android.widget.Toast; 导入org.apache.http.client.ClientProtocolException; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.ResponseHandler; 导入org.apache.http.impl.client.BasicResponseHandler; 导入org.apache.http.client.methods.HttpGet; 导入org.apache.http.impl.client.DefaultHttpClient; 导入android.app.Activity; 导入android.os.Bundle; 公共课休息延长活动时间{ /**在首次创建活动时调用*/ 字符串URL=”http://sc2.mystreamserver.com:8050/admin.cgi?mode=viewxml"; 字符串结果=”; 字符串deviceId=“xxxxx”; final String tag=“您的Logcat标签:”; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); /**首次创建活动时调用。*/ callWebService(); 最终EditText txtSearch=(EditText)findViewById(R.id.txtSearch); txtSearch.setOnClickListener(新的EditText.OnClickListener(){ public void onClick(视图v){txtSearch.setText(“”;} }); 最终按钮btnSearch=(按钮)findViewById(R.id.btnSearch); btnSearch.setOnClickListener(新建按钮.OnClickListener(){ 公共void onClick(视图v){ 字符串查询=txtSearch.getText().toString(); } }); }//结束onCreate() public void callWebService(){ HttpClient HttpClient=新的DefaultHttpClient(); HttpGet请求=新的HttpGet(URL); request.addHeader(“用户名”,“用户名”); request.addHeader(“pass”和“); ResponseHandler=new BasicResponseHandler(); 试试{ 结果=httpclient.execute(请求,处理程序); }catch(ClientProtocolException e){ 结果=e.toString(); }捕获(IOE){ 结果=e.toString(); } httpclient.getConnectionManager().shutdown(); Log.i(标签、结果); Toast.makeText(rest.this,result,Toast.LENGTH_SHORT).show(); }//end callWebService() }
我没有得到结果。请帮助获取结果……请尝试此操作Android rest Web服务问题 package com.rest; 导入java.io.IOException; 导入android.app.Activity; 导入android.os.Bundle; 导入android.util.Log; 导入android.view.view; 导入android.widget.Button; 导入android.widget.EditText; 导入android.widget.Toast; 导入org.apache.http.client.ClientProtocolException; 导入org.apache.http.client.HttpClient; 导入org.apache.http.client.ResponseHandler; 导入org.apache.http.impl.client.BasicResponseHandler; 导入org.apache.http.client.methods.HttpGet; 导入org.apache.http.impl.client.DefaultHttpClient; 导入android.app.Activity; 导入android.os.Bundle; 公共课休息延长活动时间{ /**在首次创建活动时调用*/ 字符串URL=”http://sc2.mystreamserver.com:8050/admin.cgi?mode=viewxml"; 字符串结果=”; 字符串deviceId=“xxxxx”; final String tag=“您的Logcat标签:”; @凌驾 创建时的公共void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); /**首次创建活动时调用。*/ callWebService(); 最终EditText txtSearch=(EditText)findViewById(R.id.txtSearch); txtSearch.setOnClickListener(新的EditText.OnClickListener(){ public void onClick(视图v){txtSearch.setText(“”;} }); 最终按钮btnSearch=(按钮)findViewById(R.id.btnSearch); btnSearch.setOnClickListener(新建按钮.OnClickListener(){ 公共void onClick(视图v){ 字符串查询=txtSearch.getText().toString(); } }); }//结束onCreate() public void callWebService(){ HttpClient HttpClient=新的DefaultHttpClient(); HttpGet请求=新的HttpGet(URL); request.addHeader(“用户名”,“用户名”); request.addHeader(“pass”和“); ResponseHandler=new BasicResponseHandler(); 试试{ 结果=httpclient.execute(请求,处理程序); }catch(ClientProtocolException e){ 结果=e.toString(); }捕获(IOE){ 结果=e.toString(); } httpclient.getConnectionManager().shutdown(); Log.i(标签、结果); Toast.makeText(rest.this,result,Toast.LENGTH_SHORT).show(); }//end callWebService() },android,Android,我没有得到结果。请帮助获取结果……请尝试此操作 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;
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()
}
}糟糕的问题是糟糕的。努力编辑并解释你的问题。如果我想发送一个大文件(可能1mb)而不是字符串内容,你会怎么做?convertStreamToString需要不同还是分开?什么将取代Stringbuilder?
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();