使用REST进行客户端 - 服务器通信

时间:2011-08-19 14:43:52

标签: android

当我尝试使用以下代码将数据从客户端(android)发送到服务器(java)时,我遇到了问题。

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

... 虽然我添加了我想要发送到服务器的变量,但似乎服务器没有收到任何数据(来自客户端)。你知道问题可能是什么吗? 此外,我希望服务器知道从哪个客户端(IP地址)收到消息。我想要做的是在客户端使用InetAddress并通过客户端请求将客户端地址发送到服务器,但这样做我得到127.0.0.1。我还没有真正理解127.0.0.1(loopback接口)是什么,但我想这是因为我在模拟器中运行我的应用程序(在同一台PC上我也运行我的服务器)。在使用InetAddress.getLocalHost()的真实Android设备(手机)中,它会返回机器的实际IP吗?还有另一种方法吗?谢谢!

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

}

1 个答案:

答案 0 :(得分:0)

您可以更改以下行

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

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

然后在服务器端创建一个名为“myRequestPage.php”的页面

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

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

现在,如果浏览器中显示某些内容('http://yourdomain/myRequestPage.php'),您确定服务器已收到数据。