0

I have a problem when i am trying to send data from the client(android) to the server(java) using the following code.

 Log.v(TAG, "Trying to Login");
HttpClient client = new DefaultHttpClient();
EditText etxt_user = (EditText) findViewById(R.id.username);
EditText etxt_pass = (EditText) findViewById(R.id.password);
String username1 = etxt_user.getText().toString();
String password1 = etxt_pass.getText().toString();
HttpPost httppost = new HttpPost("http://10.0.2.2:8888");
Log.v(TAG, "message1");         
//add your Data
List< BasicNameValuePair > nvps = new ArrayList< BasicNameValuePair >();
nvps.add(new BasicNameValuePair("username", username1));
nvps.add(new BasicNameValuePair("password", password1));

try {
      UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
      httppost.setEntity(p_entity);
      //Execute HTTP Post Request
      HttpResponse response = client.execute(httppost);

      Log.v(TAG,"message2");
      Log.v(TAG, response.getStatusLine().toString());
      HttpEntity responseEntity = response.getEntity();

... although I add the variables that I want to send to the server it seems that the server does not receive any data (from the client).Do you know what the problem might be? Also i would like the server to know from which client(ip address) the message was received. What i have think to do is to use the InetAddress in the client side and via a client request send the client address to the server, but doing this i get the 127.0.0.1. I haven't really understand what the 127.0.0.1(loopback interface) is but i suppose that it is because i run my application in an emulator (in the same pc i run my server too). In a real android device(mobile phone) using the InetAddress.getLocalHost() will it return the actual ip of the machine? Is there another way to do this? Thank you!

public Source invoke(Source request){
    String replyElement = new String("hello world");
    StreamSource reply = new StreamSource(new StringReader(replyElement));
    String replyElement2 = new String("hello world 2");
    StreamSource reply2 =  new StreamSource(new StringReader(replyElement2));
    String amount = null;
    if (ws_ctx == null)throw new RuntimeException("DI failed on ws_ctx.");
    if (request == null) {
        System.out.println("Getting input from query string");
        // Grab the message context and extract the request verb.
        MessageContext msg_ctx = ws_ctx.getMessageContext();
        String x = msg_ctx.toString();
        System.out.println("The value" + x + "was received from the client");
        String http_verb = (String)msg_ctx.get(MessageContext.HTTP_REQUEST_METHOD);
        System.out.println(http_verb);
        String query = (String)msg_ctx.get(MessageContext.QUERY_STRING);
        System.out.println("Query String = " + query);   
        if(query == null)
        {
            System.out.println("The query variable has zero value!!!!!");
        }
        else
        {
            System.out.println("The value of the query variable is:" + query);
        }

        http_verb = http_verb.trim().toUpperCase()

    } else {
        System.out.println("Getting input from input message");
        Node n = null;
        if (request instanceof DOMSource) {
            n = ((DOMSource) request).getNode();
        } else if (request instanceof StreamSource) {
            StreamSource streamSource = (StreamSource) request;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource inputSource = null;
            if (streamSource.getInputStream() != null) {
                inputSource = new InputSource(streamSource.getInputStream());
            } else if (streamSource.getReader() != null) {
                inputSource = new InputSource(streamSource.getReader());
            }
            n = db.parse(inputSource);
        } else {
            throw new RuntimeException("Unsupported source: " + request);
        }

    }

return reply2;
}
catch(Exception e){
    e.printStackTrace();
    throw new HTTPException(500);
}

}

4
  • How do you know that the problem is in the client and not in the server?
    – aromero
    Aug 19, 2011 at 15:46
  • Also, if you want to know the client ip, get it directly in the server, there is no point in making the client send it to the server.
    – aromero
    Aug 19, 2011 at 15:47
  • When I send the data from the client through the URL like this HttpPost httppost = new HttpPost("10.0.2.2:8888"+"?username=" + username1 + "&password=" + password1); then it works fine! I add the code of my server above, but even if my server code is not correct, and can't handle the received request in an appropriate way, the server has to receive the request from the client as it is, instead my server receives a null request. Sorry, its the first time i am using REST and i am trying to understand.
    – anna
    Aug 19, 2011 at 17:09
  • I don't know how to read the ip from the server in REST but i will search about it. If you have any suggestions I will appreciate it! Thank you!
    – anna
    Aug 19, 2011 at 17:09

1 Answer 1

0

You may change the following line

    HttpPost httppost = new HttpPost("http://10.0.2.2:8888");

to

    HttpPost httppost = new HttpPost("http://10.0.2.2/myRequestPage.php");

then in the server side create a page named 'myRequestPage.php'

    .
    .
    .
    $username = $_POST['username'];
    $userpassword = $_POST['password']

    echo 'User Name: '.$username.'    Password: ".$userpassword ; 

Now if there is something displaying in the browser ('http://yourdomain/myRequestPage.php' ) you are sure that the data is received by server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.