0

I m implementing a REST based HTTP server in Android. The server responds for GET, DELETE and POST requests. Two android devices communicate using HTTP Post (I m using a service, where a device keeps listening on a port and post to next device and this keeps going on).

I m testing the GET and DELETE using Mozilla Poster. Should I add a separate socket/port to handle the same? Because when I try now, sometimes I get timeout error or no response found. However, I am able to see server response in Logcat window. Please help me. Code to handle GET request:

if(method.equals("GET"))
 {
   if(checkFileExisting())
     {
       BufferedReader reader = new BufferedReader(new FileReader(new   File(getFilesDir()+File.separator+"script.json")));
       String read;
       StringBuilder builder = new StringBuilder("");
         while((read = reader.readLine()) != null)
          {
            builder.append(read);
          }
       String JSONContents = builder.toString();
       reader.close();
       JSONObject jsonObject;
  try {
        jsonObject = new JSONObject(JSONContents);
        String name = jsonObject.getString("name");
        JSONObject stateObject = jsonObject.getJSONObject("state");
        String stateValue = stateObject.getString("value"); 
          if(name.equals(target))
           {
              HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
             response.setEntity(new StringEntity("State is:" + stateValue));
             conn.sendResponseHeader(response);
             conn.sendResponseEntity(response);
            }
           else
           {                    
             HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
             response.setEntity(new StringEntity("The requested resource " + target + " could not be found due to mismatch!!"));
             conn.sendResponseHeader(response);
             conn.sendResponseEntity(response);
            }
        } catch (JSONException e) {
          e.printStackTrace();
       }
  }
     else
       {
           HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
           response.setEntity(new StringEntity("The requested resource " + target + " could not be found!!"));
           conn.sendResponseHeader(response);
           conn.sendResponseEntity(response);
       }                    
}   
user1741274
  • 759
  • 3
  • 13
  • 25
  • 2
    post some code from your server handlers – njzk2 Dec 11 '12 at 08:47
  • that's just the get, right ? would you mind indenting a little? Do you already have something for the DELETE ? – njzk2 Dec 11 '12 at 09:03
  • Yes I have Delete in a similar way and also Post that posts it to the next device after the socket gets connected. I will edit the code. @njzk2 – user1741274 Dec 11 '12 at 09:06
  • so, if the question is to know if you are supposed to use a different port for different HTTP methods, the answer is clearly no. That's not how it works. You http server listens on 1 port (usually 80, but then it is up to you) and that's enough. timeouts can be related to plenty of causes. do you have an example ? – njzk2 Dec 11 '12 at 09:09
  • When I use the URL: http ://192.168.1.104:5000/average and do a GET request in Mozilla Poster, I m able to see the response logs in Logcat, but the response is not seen mostly. Poster shows "Receiving" and after sometime I am getting "No GET response was received or the request timed out". Ya that was my question, if I have to use different socket connections and port numbers in server to respond to different methods. @njzk2 – user1741274 Dec 11 '12 at 09:13
  • `conn.sendResponseHeader(response);` looks odd. are you sure of this ? – njzk2 Dec 11 '12 at 10:21
  • http://hc.apache.org/httpcomponents-core-ga/tutorial/pdf/httpcore-tutorial.pdf I m referring to this tutorial for using HTTP. – user1741274 Dec 11 '12 at 11:13
  • @njzk2 Can you please suggest me some example to make my server non-blocking? – user1741274 Dec 12 '12 at 12:32
  • is the issue the response time of the server ? because of the treatment of the request ? – njzk2 Dec 12 '12 at 12:51
  • The issue is response time in Poster. I m able to see the server's response in logs as soon as I issue the GET/DELETE requests. But as the devices keep Posting a file(HTTP Post in a service), thought it may be blocking. @njzk2 – user1741274 Dec 12 '12 at 13:04
  • i don't understand what you are trying to do. uploading a file has to block both client and server at some point, by construction. – njzk2 Dec 12 '12 at 14:10
  • Ya I understand that. But can the GET/DELETE requests be handled in separate threads or some Async handler? I want to know, if it is possible/correct to do like this? @njzk2 – user1741274 Dec 12 '12 at 14:19
  • i don't understand in what thread is your server running. Don't you have a thread pool and each connection is opened in one of these ? – njzk2 Dec 12 '12 at 14:36
  • Actually I have not implemented that. Do you mean I can have separate threads to handle each of these methods (GET/DELETE/POST), by using the same socket/port? @njzk2 – user1741274 Dec 13 '12 at 07:08
  • check the async service on page 30, i think that's what it does. – njzk2 Dec 13 '12 at 08:20
  • @njzk2 Ya I m referring to that. But please let me know if I can use the same socket/port to handle all HTTP methods? – user1741274 Dec 14 '12 at 07:56
  • @njzk2 Hi, I tried and found just now that, HttpAsyncService does not exist in Android. I was getting Fatal exception:Thread -11. org.apache.http.nio.protocol.HttpAsyncService is not present in Android. Could you please suggest me how can I implement non-blocking HTTP server in android? Is it anything to do with multithreading, AsyncTask or message queue? – user1741274 Dec 17 '12 at 10:51

1 Answers1

0

The link http://www.integratingstuff.com/2011/10/24/adding-a-webserver-to-an-android-app/ has a very good example. I missed conn.close() in my code.

user1741274
  • 759
  • 3
  • 13
  • 25