如何Android的XML发送到PHP的Web服务,才能使用Web服务的Andr​​oid发送电子邮件 [英] How to send android xml to php web service in order to send email using web service android

查看:168
本文介绍了如何Android的XML发送到PHP的Web服务,才能使用Web服务的Andr​​oid发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发送XML(设计机器人)到PHP Web服务,那么就需要使用Web服务来发送电子邮件,也希望得到电子邮件的JSON响应
我在android的初学者,请帮我...
谢谢


解决方案

 公共类JSONParser {
    静态InputStream为= NULL;
    静态的JSONObject jObj = NULL;
    静态JSON字符串=;    公共JSONParser(){    }
    公众的JSONObject makeHtt prequest(URL字符串,字符串的方法,
                                      清单<&的NameValuePair GT; PARAMS){
        尝试{
            如果(方法==POST){
                DefaultHttpClient的HttpClient =新DefaultHttpClient();
                HttpPost httpPost =新HttpPost(URL);
                httpPost.setEntity(新UrlEn codedFormEntity(PARAMS));                HTT presponse HTT presponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = HTT presponse.getEntity();
                是= httpEntity.getContent();            }否则如果(方法==GET){
                DefaultHttpClient的HttpClient =新DefaultHttpClient();
                字符串中的paramString = URLEn codedUtils.format(参数,可以UTF-8);
                网址+ =? +中的paramString;
                HTTPGET HTTPGET =新HTTPGET(URL);                HTT presponse HTT presponse = httpClient.execute(HTTPGET);
                HttpEntity httpEntity = HTT presponse.getEntity();
                是= httpEntity.getContent();
            }        }赶上(UnsupportedEncodingException五){
            e.printStackTrace();
        }赶上(ClientProtocolException E){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }        尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                    是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                sb.append(行+\\ n);
            }
            is.close();
            JSON = sb.toString();
        }赶上(例外五){
            Log.e(缓冲区错误,错误转换结果+ e.toString());
        }
        尝试{
            jObj =新的JSONObject(JSON);
        }赶上(JSONException E){
            Log.e(JSON解析器,错误分析数据+ e.toString());
        }
        返回jObj;    }
}

然后创建的AsyncTask类将数据上传到server.Then添加此code。

  JSONParser jsonParser =新JSONParser();
@覆盖
    保护字符串doInBackground(字符串...字符串){
        字符串消息=;
        清单<&的NameValuePair GT; PARAMS =新的ArrayList<&的NameValuePair GT;();
        params.add(新BasicNameValuePair(值1值1));        JSONObject的JSON = jsonParser.makeHtt prequest(SERVER_URL,
                POST,则params);
        尝试{
                消息= json.getString(信息);
                Log.e(味精,消息);        }赶上(JSONException E){
            e.printStackTrace();
        }
        返回消息;
    }

值1发送到服务器。

I need to send xml(designed in android) to php web service and then need to use web service to send email and also want to get the response of email in json i am a beginner in android so please help me ... thanks

解决方案

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;

    }
}

Then Create AsyncTask Class to upload data to server.Then add this code.

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 send to server.

这篇关于如何Android的XML发送到PHP的Web服务,才能使用Web服务的Andr​​oid发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆