使用Android的C#Rest服务

使用Android的C#Rest服务,c#,android,C#,Android,正在尝试在我的PC和手机之间进行简单的通信测试(目前正在使用emulator进行测试) 我已经创建了一个工作正常的c#Rest服务 网址: 结果: 测试字符串 我的android应用程序中有一个调用函数,如下所示: public void call() { String URL = "http://192.168.1.100:8000/REST/client"; EditText txt = (EditText) findViewById(R.id.Textbox);

正在尝试在我的PC和手机之间进行简单的通信测试(目前正在使用emulator进行测试) 我已经创建了一个工作正常的c#Rest服务

网址: 结果:

测试字符串

我的android应用程序中有一个调用函数,如下所示:

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

} 

这段代码没有什么问题,如果我运行它,我不会看到任何Toast MSG,文本框也不会更新。但是,如果我从onclick中删除call(),则文本框将更新。似乎呼叫挂起,但即使挂起,我也不会看到文本更新吗?

假设它可以到达您的服务等,您应该使用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);
}
public void doBeginCall()
{
Toast.makeText(此“调用已开始”,Toast.LENGTH_SHORT).show();
new CallTask().execute(null);
}
公共void onCallComplete(字符串结果)
{
Toast.makeText(这个“调用完成”,Toast.LENGTH_SHORT).show();
((EditText)findViewById(R.id.Textbox)).setText(结果);
使无效();
}
类CallTask扩展了AsyncTask
{
受保护的void onPostExecute(字符串结果)
{
一次完成(结果);
}
@凌驾
受保护的TaskResult doInBackground(字符串…参数)
{           
返回调用();
}
}
公共字符串调用()
{ 
字符串URL=”http://192.168.1.100:8000/REST/client";
字符串结果=”;
HttpClient hc=新的默认HttpClient();
HttpGet请求=新的HttpGet(URL);
HttpResponse hr=null;
BasicResponseHandler=新的BasicResponseHandler();
尝试
{
hr=hc.execute(请求);
}
捕获(客户端协议例外e)
{   
e、 printStackTrace();
} 
捕获(IOE异常)
{   
e、 printStackTrace();
} 
hc.getConnectionManager().shutdown();
返回处理人。处理人响应(hr);
}
此外,您还需要为应用程序设置适当的权限。在清单xml中包括以下行:

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


不要在UI线程中运行长任务。使用ex.AsyncTask您的Toast消息可能没有显示,因为您没有对从
makeText()
返回的Toast实例调用
show()
。Dziobas试图先运行一个测试,但当我创建实际的应用程序时,我会感谢Schris-谢谢,这会显示MSG,调用会很快返回,我把文本框中的表达改为post,我发现我收到了一个IO异常。android和java新手(C#guy)诊断这个问题的最佳方法是什么?(使用带emulator的eclipse)Thanksi刚刚在我的emulator上打开了web浏览器,输入了URL,测试字符串显示正常,因此它可以连接,但在运行代码时,我仍然有一个IO错误。我将尝试实现Flying的建议,但我认为它不会有太大变化,因为调用是以相同的方式完成的。我看到您更改了请求的执行方式,所以我是以hc.execute(request)的方式执行的,但我仍然会得到IO错误异常。是什么方法抛出它?您是否在清单中设置了INTERNET权限?我不确定“INTERNET”权限是什么,因此可能不是因为emulator上的web浏览器可以访问该网站。您需要授予应用程序访问INTERNET的权限,请参阅我回答中的编辑。太棒了,谢谢,最后一件事,它显示了页面的原始结果(xml)获取值的最佳方法是什么?
<uses-permission android:name="android.permission.INTERNET"/>