【问题标题】:How to send android xml to php web service in order to send email using web service android如何将 android xml 发送到 php web 服务以便使用 web 服务 android 发送电子邮件
【发布时间】:2014-12-06 17:58:37
【问题描述】:

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

【问题讨论】:

  • 我不知道如何将整个 XML 文件发送到 Web 服务,但您可以将 XML 中的数据插入 JSON 数组并发送到服务器。如果您需要,我将向您展示示例。

标签: android xml web-services email


【解决方案1】:
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 发送到服务器。

【讨论】:

    【解决方案2】:

    您可以使用代码

    /*
    * 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多