【问题标题】:Android Regarding how to acess soap web services and parse the data using jsonAndroid关于如何访问soap web服务并使用json解析数据
【发布时间】:2011-03-09 11:00:21
【问题描述】:

我有一段关于使用肥皂网络服务和 json 解析进行登录身份验证的代码,但我很困惑它是否正确请指导我?? 如果可能,请提供代码。

这是我的代码:-

package com.devstream.http;
import java.io.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
    ImageButton b;
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL ="http:....asmx";
    private static final String SOAP_ACTION = "http://tempuri.org/LoginRequest";
    private static final String METHOD_NAME = "LoginRequest";
    protected static final String END_DOCUMENT =null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        b=(ImageButton)findViewById(R.id.ImageButton01);
        b.setOnClickListener(new OnClickListener()
        { 
            public void onClick(View v) {
                String sUserName,sPassword;
                EditText usernameEditText = (EditText)findViewById(R.id.EditText01);
                EditText passwordEditText = (EditText)findViewById(R.id.EditText02);


        if(usernameEditText == null ||passwordEditText == null){
                    Toast.makeText(getApplicationContext(), "Please Enter Valid Email and Password", Toast.LENGTH_LONG).show();
                    //Log.i("enter Correct details", null);


                }else if(usernameEditText != null || passwordEditText != null){
                    sUserName = usernameEditText.getText().toString();
                    sPassword = passwordEditText.getText().toString();
                    String string = 
                        "{\"Geninfo\": " +
                        "{\"appname\":\"MNB\"," +
                        "\"appver\":\"1.0.0\"," +
                        "\"deviceType\":\"BlackBerry\"," +
                        "\"deviceOSVersion\":\"3.0\":" +
                        "\"phoneDeviceID\":\"9150107470\"}," +
                        "\"Login\":" +
                        "{\"emailID\": " + sUserName +
                        "\"Password\": "+ sPassword + "}}"; 

                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
                    request.addProperty("JsonRequestString",string);

                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12); 

                    envelope.encodingStyle = SoapSerializationEnvelope.ENC2001; 
                    envelope.dotNet=true; 
                    envelope.setOutputSoapObject(request);

                    Log.i("Given Input",string);
                    Log.i("LoginDetail","Username : " +sUserName+ "Password :" +sPassword); 

                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    androidHttpTransport.debug=true;

                    System.out.println("request: " + androidHttpTransport.requestDump);
                    System.out.println("response:"+androidHttpTransport.responseDump);
                    androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

                    try {
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        SoapPrimitive resultstring = (SoapPrimitive) envelope.getResponse();
                        Log.i("OUTPUT",resultstring.toString()); }
                    catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 
                    next();
                }}//end else
        });
        XmlPullParserFactory factory;
        try {
            factory = XmlPullParserFactory.newInstance();
            XmlPullParser xpp = factory.newPullParser();
            String n=xpp.getText().toString();
            while( n != END_DOCUMENT ) {
                try {
                    // do your parsing element-by-element
                } catch( Throwable e ) {
                    Log.w("error while parsing file: " + e.getMessage(),""); 
                    // you may want to catch any other exceptions and do something about them
                }
            } 
        } catch (XmlPullParserException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        }
    }

    public void setUserNameText(String $emailid){
        EditText usernameEditText = (EditText)findViewById(R.id.EditText01);
        usernameEditText.setText($emailid);
    }
    public void setPasswordText(String $password){
        EditText passwordEditText = (EditText)findViewById(R.id.EditText02);
        passwordEditText.setText($password);
    }
    private void next(){ 
        Intent i=new Intent(getApplicationContext(), Home.class);
        startActivity(i);
    }
}

在 logcat 中运行时,我能够看到输入、用户名和密码,但它显示请求和响应为 null 是什么问题我无法理解,请指导我???

【问题讨论】:

  • 你能贴一些代码吗?你想做什么?
  • 你确定...我只会发布我的代码

标签: android json soap


【解决方案1】:

请按照以下代码。在这里,我在服务器上发送请求并获取该响应并在 json 中解析该响应。

public void Login(final String UserName,final String Password)
{
    try
    {
        new Thread()
        {
            public void run()
            {
                try
                {
                    HttpPost postMethod = new HttpPost("Your Url");
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                    nameValuePairs.add(new BasicNameValuePair("UserName","Your UserName");
                    nameValuePairs.add(new BasicNameValuePair("Password", "Your Password"));

                    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    DefaultHttpClient hc = new DefaultHttpClient();

                    HttpResponse response = hc.execute(postMethod);
                    HttpEntity entity = response.getEntity();

                    // If the response does not enclose an entity, there is no need
                    // to worry about connection release

                    if (entity != null) 
                    {
                        InputStream inStream = entity.getContent();
                        result= convertStreamToString(inStream);
                        jsonObject = new JSONObject(result);
                        responseHandler.sendEmptyMessage(0);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }.start();

        responseHandler = new Handler() 
        {                               
            public void handleMessage(Message msg) 
            {
                super.handleMessage(msg);
                try 
                {
                    Log.i("-------------------- Response",result);
                    if(jsonObject.getJSONObject("response").getString("msg").equalsIgnoreCase("Success"))
                    {

                        Log.i("Login","--- Login Success");
                    }
                } 
                catch (Exception e) 
                {
                    e.printStackTrace();
                }                       
            }
        };
    }
    catch(Exception e)
    {

    }
}

public static String convertStreamToString(InputStream is)
{
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuilder sb = new StringBuilder();

   String line = null;
   try 
   {
       while ((line = reader.readLine()) != null) 
       {
           sb.append(line + "\n");
       }
   } 
   catch (IOException e) 
   {
       e.printStackTrace();
   } 
   finally 
   {
       try 
       {
           is.close();
       } 
       catch (IOException e) 
       {
           e.printStackTrace();
       }
   }
   return sb.toString();

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    相关资源
    最近更新 更多