2011-03-08 91 views
2

我有关于如何在Android中使用JSON集成的问题。例如,我有一个登录页面,我想使用SOAP Web服务进行身份验证来导航它。我想使用JSON集成,我该怎么做?有谁能够帮助我?如果你有一个例子,请提供它。使用SOAP webservices进行Android + JSON解析

这是我的代码,它在logcat中显示输出。现在如何在模拟器上检索?

package com.temp; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.Writer; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLConnection; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.BasicResponseHandler; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.HttpProtocolParams; 
import org.apache.http.util.EntityUtils; 
import org.kxml2.kdom.Document; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 


import android.app.Activity; 
import android.os.Bundle; 
import android.provider.Browser; 
import android.text.StaticLayout; 
import android.util.Log; 
import android.widget.TextView; 



public class temp extends Activity{ 
    private static final String NAMESPACE = "http://tempuri.org/"; 

    private static final String URL ="http://";  

    private static final String SOAP_ACTION = "http://tempuri.org/Login"; 

    private static final String METHOD_NAME = "Login"; 
    TextView tv; 
    String string; 
    @Override 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     string = 
      "{\"Geninfo\": " + 
      "{\"appname\":\"\"," + 
      "\"appver\":\"1.0.0\"," + 
      "\"deviceType\":\"\"," + 
      "\"deviceOSVersion\":\"3.0\"," + 
      "\"phoneDeviceID\":\"0\"}," + 
      "\"Login\":" + 
      "{\"emailID\":\"\",[email protected]"+ 
      "\"Password\":\"123456\"}}"; 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false); // Comment: 1 

     HttpPost httppost = new HttpPost(URL); // Comment: 2 

     // connection.setDoOutput(true); 

     URL u; 
     URLConnection uc; 
     try { 
      u = new URL(URL); 
      uc = u.openConnection(); 


      HttpURLConnection connection = (HttpURLConnection) uc; 
      StringBuffer soap = new StringBuffer(); 
      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Host","191.118.0.4"); 
      connection.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8"); 
      connection.setRequestProperty("Content-Length",String.valueOf(soap.length())); 

      InputStream is = connection.getInputStream(); 

      //HttpPost httpget = new HttpPost(URL); 
      connection.setRequestMethod(soap.toString()); 
      //DefaultHttpClient client = new DefaultHttpClient(); 
      // httppost.setHeader("POST","/makenewbuddies/mobileapp/APImobilelogin.asmx HTTP/1.1"); 
      // httppost.setHeader("SOAPAction", SOAP_ACTION); 
      // httppost.setHeader("Content-Type", "application/soap+xml; charset=utf-8"); 
      // Comment: 4 
      soap.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
      soap.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">") ; 
      soap.append("<soap:Body>"); 
      soap.append("<LoginRequest xmlns=\"http://tempuri.org/\">"); 
      soap.append("<JSONRequestString>"+string+"</JSONRequestString>"); 

//soap.append("<strPassword>"+password+"</strPassword>"); 
      // soap.append("<strXml_In>"); 
      // soap.append("&lt;XML-GetDetailsOfAMobileArticle-IN DATACLASS= 'DetailsOfAMobileArticle'&gt; &lt;CONDITION ARTICLE_ID= '"+articleid+"' CALLER='' USER_ID='"+userid+"'/&gt; &lt;/XML-GetDetailsOfAMobileArticle-IN&gt;"); 
      // soap.append("</strXml_In>"); 
      soap.append("</LoginRequest>"); 
      soap.append("</soap:Body>"); 
      soap.append("</soap:Envelope>"); 
      Log.d("soap value1 is",soap.toString());  

      // String len=String.valueOf(soap.length()); 
      // httppost.setHeader("Content-Length", String.valueOf(soap.length())); 
      try { 
       // StringEntity strent= new StringEntity(soap.toString()); 
       //  strent.setContentType("application/soap+xml; charset=utf-8"); 

       // httppost.setEntity(strent); 
       ResponseHandler<String> response = new BasicResponseHandler(); 
       String responseBody = httpclient.execute(httppost,response); 


       Log.v("soap response is",responseBody); 

       // System.out.println("Thanks God" + responseBody); 
      } catch(Exception e) { 
       e.printStackTrace(); 
       System.out.println("Exception Thanks God : " + e.toString()); 

      } 
      OutputStream out = connection.getOutputStream(); 

      Writer wout = new OutputStreamWriter(out); 
      wout.write(soap.toString()); 

      wout.flush(); 

      wout.close(); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

      //InputStream in = connection.getInputStream(); 

      String result; 
      //int c; 
      while ((result=rd.readLine()) != null) { 

       System.out.println(result); 

      } }catch (MalformedURLException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 



      catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       HttpClient client = new DefaultHttpClient(); 

       HttpGet get = new HttpGet(URL); 
       HttpResponse responseGet = client.execute(get); 
       HttpEntity resEntityGet = responseGet.getEntity(); 
       if (resEntityGet != null) { 
          //do something with the response 
          Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet)); 
         } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
       } 



    } 
+3

欢迎计算器。用完整的语法正确的英语句子来构建你的问题将大大提高你获得有用答案的机会。另外,为了得到对你的问题有用的答案,你需要更具体一些;单独采取这些都看起来像你可以只为谷歌的东西。 JSON解析内置于Android(搜索文档),并且有数以千计的关于SOAP的Java教程和至少一些与Android相关的教程。 – 2011-03-08 05:14:33

+0

soap webservice在服务器端实现。你与它无关。您只需将数据以json格式发布到服务器,它就会对您做出回应。 – 2011-03-08 05:14:53

回答

2

Android SDK包含JSON实现。您将正文格式设置为JSON时提交您的请求。如果该请求被接受,您的服务将返回响应的主体将以JSON格式。之后,你根本就通过声明JSON对象如下解析:

JSONObject json = new JSONObject(rawJson); 

详见这里http://goo.gl/iVXI4或只是谷歌它