如何将android xml发送到php web服务以便使用web服务android发送电子邮件

时间:2014-12-06 17:58:38

标签: android xml web-services email

我需要将xml(在android中设计)发送到php web服务,然后需要使用web服务发送电子邮件,并且还想在json中获取电子邮件的响应 我是android的初学者所以请帮帮我... 谢谢

2 个答案:

答案 0 :(得分:0)

你可以使用危险代码

/*
* Getting the XML ready for sending
*/
String xml = getXml(); // YOUR XML STRING  
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add( new BasicNameValuePair("XML",xml) );
/*
* Getting ready to send and receive from server
*/
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost("/your/service/url");
post.setEntity(new UrlEncodedFromEntity(nvps));
post.addHeader("Content-Type","application/xml");
post.addHeader("Accept","application/json");
/*
* Sending the file and waiting for response
*/
HttpResponse response = httpClient.execute(post);
/*
* Getting the JSON from the response
*/
if( response.getStatusLine().getStatusCode() == 200 ){
    String json = EntityUtils.toString(response.getEntity());
    // PROCESS RESPONSE HERE
}  

请注意我是从记忆中写的。我可能在这里和那里犯了错误。您现在可以使用GSON反序列化您在回复时收到的JSON。

答案 1 :(得分:0)

public class JSONParser {
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    public JSONParser() {

    }
    public JSONObject makeHttpRequest(String url, String method,
                                      List<NameValuePair> params) {


        try {
            if(method == "POST"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        return jObj;

    }
}

然后创建AsyncTask类以将数据上传到服务器。然后添加此代码。

JSONParser jsonParser = new JSONParser();


@Override
    protected String doInBackground(String... strings) {
        String message="";        
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("value1", value1));

        JSONObject json = jsonParser.makeHttpRequest(SERVER_URL,
                "POST", params);
        try {
                message=json.getString("message");
                Log.e("msg",message);

        }catch (JSONException e) {
            e.printStackTrace();
        }
        return message ;
    }

value1发送到服务器。

相关问题