简体   繁体   中英

consume C# Rest Service from Android

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

URL: http://192.168.1.100:8000/REST/client Result:

TEST STRING!

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

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?

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

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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM