从Android的消耗C#REST服务 [英] consume C# Rest Service from Android

查看:108
本文介绍了从Android的消耗C#REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图做我的电脑和手机之间的简单通讯测试(目前模拟器中进行测试) 我创建了一个C#休息服务,工程确定

trying to do a simple communication test between my PC and phone (currently testing with emulator) I have created a c# Rest service that works ok

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

测试字符串!

我在我的Andr​​oid应用程序呼叫的功能,看起来是这样的:

I have a Call function in my android app that looks like this:

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

} 

我把这种从一个按钮,

i call this from a button,

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

几个问题,与此code由于是,如果我运行它,我没有看到任何吐司封邮件和文本框心不是更新。然而,如果我删除了()的调用由的onclick,文本框将被更新。它仿佛在呼叫悬挂,但即使是,wouldnt我看到的文字反正更新?

few problems, with this code as is, if i run it i dont see any Toast msgs and the textbox isnt updated. if however i remove the call() from the onclick, the textbox is updated. it seems as though the call is hanging, but even if it was, wouldnt i see the text updated anyways?

推荐答案

假设它可以达到您的服务等,您应该用AsyncTasks这样做,并呼吁无效,以确保更新显示在屏​​幕上。此外,你必须调用 .show() makeText()。我没有测试过这一点,但它应该是正确的想法。

Assuming that it can reach your service etc, you should be doing it with AsyncTasks and calling invalidate to make sure the updates are shown on the screen. Also, you have to call .show() on makeText(). I haven't tested this but it should be the right idea.

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以下行:

Also, you need to have the proper permissions set up for your app. Include the following line in your manifest xml:

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

这篇关于从Android的消耗C#REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆