【问题标题】:consume C# Rest Service from Android从 Android 使用 C# Rest Service
【发布时间】:2012-01-24 15:14:23
【问题描述】:

尝试在我的电脑和手机之间进行简单的通信测试(目前正在使用模拟器进行测试) 我创建了一个可以正常工作的 c# Rest 服务

网址:http://192.168.1.100:8000/REST/client 结果:

测试字符串!

我的 Android 应用中有一个 Call 函数,如下所示:

public void call() 
{ 
    String URL = "http://192.168.1.100:8000/REST/client";
    EditText txt = (EditText) findViewById(R.id.Textbox);
    String Result = "";
    Toast.makeText(Helloworld.this, "STARTING", Toast.LENGTH_SHORT);
    HttpClient hc = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);
    ResponseHandler<String> handler = new BasicResponseHandler();
    try{
        Result = hc.execute(request,handler);
    }
    catch (ClientProtocolException e) {   
        e.printStackTrace();   
    } catch (IOException e) {   
        e.printStackTrace();   
    } 
    txt.setText(Result);
    Toast.makeText(Helloworld.this, Result, Toast.LENGTH_SHORT);
    hc.getConnectionManager().shutdown();

} 

我通过一个按钮调用它,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            EditText txt = (EditText) findViewById(R.id.Textbox);
            txt.setText("calling!", TextView.BufferType.EDITABLE );
            call();
        }
        });

很少有问题,使用此代码,如果我运行它,我看不到任何 Toast 消息并且文本框没有更新。但是,如果我从 onclick 中删除 call(),则会更新文本框。似乎通话挂起,但即使挂起,我也不会看到文本更新吗?

【问题讨论】:

  • 不要在 UI 线程中运行长任务。使用前。异步任务
  • 您的 Toast 消息可能没有显示,因为您没有在从 makeText() 返回的 Toast 实例上调用 show()
  • Dziobas,试图先运行一个测试,但是当我创建实际的应用程序时,我会感谢
  • 克里斯 - 谢谢,它显示了消息和呼叫快速返回,我将 exptions 更改为在文本框中发布,我发现我得到了一个 IO 异常。 android 和 java 新手(C# 家伙)什么是诊断这个的最佳方法? (用模拟器使用eclipse)谢谢
  • 我刚刚在我的模拟器上打开了网络浏览器,输入了 URL 并且测试字符串正常,所以它可以连接,但是当我运行代码时仍然有一个 IO 错误。我将尝试实施 Flying 的建议,但我认为这不会有太大变化,因为通话方式相同。

标签: c# android


【解决方案1】:

假设它可以访问您的服务等,您应该使用 AsyncTasks 并调用 invalidate 以确保更新显示在屏幕上。此外,您必须在makeText() 上致电.show()。我没有对此进行测试,但它应该是正确的想法。

public void doBeginCall()
{
  Toast.makeText(this, "Call started", Toast.LENGTH_SHORT).show();
  new CallTask().execute(null);  
}

public void onCallComplete(String result)
{
    Toast.makeText(this, "Call complete", Toast.LENGTH_SHORT).show();
    ((EditText)findViewById(R.id.Textbox)).setText(result);
    invalidate();
}

class CallTask extends AsyncTask<String, String, String> 
{

    protected void onPostExecute(String result) 
    {
        onCallComplete(result);
    }

    @Override
    protected TaskResult doInBackground(String... params) 
    {           
        return call();
    }
}

public String call() 
{ 
    String URL = "http://192.168.1.100:8000/REST/client";
    String Result = "";
    HttpClient hc = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);
    HttpResponse hr = null;
    BasicResponseHandler handler = new BasicResponseHandler();
    try
    {
        hr = hc.execute(request);
    }
    catch (ClientProtocolException e) 
    {   
        e.printStackTrace();   
    } 
    catch (IOException e) 
    {   
        e.printStackTrace();   
    } 
    hc.getConnectionManager().shutdown();
    return handler.handleResponse(hr);
}

此外,您需要为您的应用设置适当的权限。在清单 xml 中包含以下行:

<uses-permission android:name="android.permission.INTERNET"/>

【讨论】:

  • 我看到你改变了请求的完成方式,所以我这样做了 hc.execute(request) 并且我仍然得到 IO 错误异常。
  • 什么方法扔的?您是否在清单中设置了 INTERNET 权限?
  • 我不确定“INTERNET”权限是什么,所以可能不是因为模拟器上的网络浏览器可以正常访问网站。
  • 您需要授予您的应用访问互联网的权限,请参阅我的回答中的编辑。
  • 太棒了,谢谢,最后一件事,它显示了页面的原始结果 (xml) 获取价值的最佳方式是什么? schemas.microsoft.com/2003/10/Serialization/">TESTSTRING!
猜你喜欢
  • 2011-09-30
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多