G
G
Gregary2014-12-16 05:40:11
Android
Gregary, 2014-12-16 05:40:11

How to test Android client for ASP.net web api?

Developed a small application on asp.net web api. I test it in the browser, I get Json, everything is fine. Developed an Android client that just needs to send a Get request. But I can't test. When the button is clicked, the method is executed, but nothing happens when viewed in Fiddler. How do you know if everything is working or not working? Here is the client code

public class HttpClientTest extends Activity
{

    EditText Resultat;
    sendFormTask sendForm;
    Button btnOk;

    static public String url = "http://localhost:3815/api/values/CreateBook";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_client);
        btnOk = (Button) findViewById(R.id.btnOk);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.http_client, menu);
        return true;
    }

    public void sendMessage (View view)
    {
        String someUrl;
        someUrl = "http://localhost:3815/api/values/CreateBook";
        sendData("http://localhost:3815/api/1");
    }

    public void sendData(String URLString)
    {
        new sendFormTask().execute(URLString);
    }

    class sendFormTask extends AsyncTask<String, Boolean, Boolean>
    {
        @Override
        protected Boolean doInBackground(String... urls) {
            // Create a new HttpClient and Post Header
            String URLString = urls[0];
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet request = new HttpGet(URLString);

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("Балто", "1"));


                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(request);
                Log.e("DEBUG:", "REQUEST SENT");
            }

            catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            return true;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anyd3v, 2014-12-16
@anyd3v

At least from the code: you are not starting the task correctly

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB)
    myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
    myTask.execute();

well, try to explicitly register OnClickListener for btnOk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question