I’m working on an android app that has to send and receive information from a 3rd party server. I’m not experienced at this at all, so bear with me if I give too much/too little/not the right kind of information at first.
The API that was provided to send information has the format
https://methodurl.com/username=user&password=pass&key=key&info=infotosend
When I put this URL into my desktops browser, it returns a string, which I’m also trying to get. At least, I’m assuming all it returns is a string, since if I look at the page source, there is just a string, such as “200 OK” or “401 Unauthorized”.
The code I’m using is what I can glean from the web what I’m supposed to use. Unfortunately, for all I know, I’m writing code to do something very different than what I want. I’ve never written code to interact with a server before, so this is all new to me.
Here’s my code:
TextView urlView = (TextView)findViewById(R.id.url_view);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(siteToSubmit);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
String serverResponse = client.execute(request, responseHandler);
urlView.setText(serverResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logcat in eclipse (when app is running on the emulator) is
java.net.UnknownHostException
The way I understand the code to be working is I’m creating a client that will execute on a url (like would happen when I hit enter in the URL bar on a browser), a request for that client to execute on, and a handler to receive the response from the server, which is set to be recieved as a string. I am then trying to set that string as the text in a TextView to verify that I am getting the proper response. Right now, I just get the log error, and urlView does not change. Any ideas why? Am I understanding the process correctly?
Thanks!
Add the uses-permission
android.permission.INTERNETto the manifest file.