2011-03-09 55 views
-2

我有一段关于使用soap web服务和json解析进行登录认证的代码,但我很困惑,这是否正确,请指导我?如果可能的话请提供我的代码 。Android关于如何使用soap web服务和解析数据使用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中运行这个我能看到输入用户名和密码,但它显示了请求和响应空是什么问题我无法理解,请引导我???

+0

你可以发布一些代码吗?你想做什么? – longhairedsi 2011-03-09 11:07:34

+0

你确定...我只会发布我的代码 – chandu489 2011-03-09 11:25:15

回答

0

请按照下面的代码。在这里,我发送服务器上的请求并获取该响应并在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(); 

}