2

I have a question regarding how to use JSON integration in Android. For example I have a login page I want get authenticated using SOAP webservices to navigate it. I want to use JSON integration, how can I do it? Can anybody help me? If you have an example, please provide it.

This is my code which displays output in logcat. Now how to retrieve on emulator?

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\":\"\",abc@gmailcom"+
            "\"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();
        }
                }



    }
2
  • 3
    Welcome to stackoverflow. Framing your question in complete grammatically correct English sentences will go a long way towards improving your chance of getting a helpful response. Also, in order to get helpful answers to your question, you need to be a little more specific; taken individually these all seem like things you could just Google for. JSON parsing is built in to Android (search the docs), and there are thousands of tutorials on SOAP in Java and at least a few that are Android specific. Commented Mar 8, 2011 at 5:14
  • soap webservice is implemented on server side. you have nothing to do with it. you just have to post data to server in json format and it will respond to you. Commented Mar 8, 2011 at 5:14

1 Answer 1

2

Android SDK includes JSON implementation. You submit your request where body is formatted as JSON. If that request is accepted your service will return response where body of the response will be in JSON format. After that you simply do parsing by declaring JSON object as follows:

JSONObject json = new JSONObject(rawJson);

See more here http://www.ibm.com/developerworks/web/library/x-andbene1/?ca=drs- or just Google for it

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.