2011-03-09 4 views
-2

을 사용하여 데이터를 구문 분석 나는 비누 웹 서비스 및 json 구문 분석을 사용하여 로그인 인증에 관한 코드를 가지고 있지만 옳지는 혼란 스럽습니까? 가능한 경우 코드를 입력하십시오.안드로이드 비누 웹 서비스를 처리하는 방법 및 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); 
    } 
} 

로그 캣이를 실행할 때 입력, 사용자 이름과 암호를 볼 수 있어요하지만 문제가 무엇인지 요청 및 응답 널 (null)이 나를 인도 해주십시오 이해할 수 없습니다입니다 보여줍니다 - : 여기

내 코드입니다 ???

+0

당신이 몇 가지 코드를하시기 바랍니다 게시 할 수 있습니까? 너 뭐하려고? – longhairedsi

+0

예. 확신합니다 ... 내 코드 만 게시합니다. – chandu489

답변

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(); 

}

관련 문제