2014-12-06 77 views
0

我需要發送xml(在android中設計)到php web服務,然後需要使用web服務發送電子郵件並且還想獲得電子郵件的JSON 我在android系統是一個初學者的響應,請幫我... 感謝如何發送android xml到php web服務以便使用web服務發送電子郵件android

+0

我不知道如何將整個XML文件發送到Web服務,但您可以將數據插入XML到JSON數組併發送到服務器是可能的。如果您需要我會告訴你的例子。 – Sajithv 2014-12-06 18:04:53

回答

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發送到服務器。

0

可以HAZ格蘭代碼

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

相關問題