Android, send and receive XML via HTTP POST method

后端 未结 3 1105
臣服心动
臣服心动 2020-12-05 12:04

There is a relevant question, but I could not get the answer clearly.

I would like to POST a short xml code



        
相关标签:
3条回答
  • 2020-12-05 12:54

    Why not use Spring RestTemplate in Spring for Android?

    0 讨论(0)
  • 2020-12-05 12:56

    You can get the content of the response using:

    String responseXml = EntityUtils.toString(httpResponse.getEntity());
    

    You can then write this to a file using something like this.

    there is something wrong with receiving the response

    Since you havn't said what is wrong with receiving the response it's somewhat difficult to help you with this point.

    0 讨论(0)
  • 2020-12-05 13:01

    Ok, I have figured out soon after I posted this question. This code here works fine:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://192.168.192.131/");
    
    try {
        StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8);
        se.setContentType("text/xml");
        httppost.setEntity(se);
    
        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity resEntity = httpresponse.getEntity();
        tvData.setText(EntityUtils.toString(resEntity));        
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题